Turn from: https://msdn.microsoft.com/zh-cn/library/ms171619 (v=vs.110). aspx
The DataGridView control includes the Datagridviewbuttoncell class, which is used to display cells that have a user interface (UI) similar to a button. However, Datagridviewbuttoncell does not provide a way to disable the appearance of the button that the cell displays.
The following code example demonstrates how to customize the Datagridviewbuttoncell class to display buttons that can be displayed as disabled.This example defines a new cell type, which is derived from the DatagridviewbuttoncellDataGridViewDisableButtonCell.This cell type provides a newEnabled property, which can be set tofalseTo drag a button that is disabled in the cell.This example also defines a new column type, which displaysdatagridviewdisablebuttoncell object datagridviewdisablebuttoncolumn. datagridviewcheckboxcell in The parent datagridview determines whether the enabled property of the datagridviewdisablebuttoncell in The same row is true or false . " > In order to demonstrate this new cell and column type, the current value of each datagridviewcheckboxcell in parent DataGridView determines the peer in datagridviewdisablebuttoncell enabled property is true or false .
note |
datagridviewcell or datagridviewcolumn and add new Properties to the derived class, being sure to override the Clone method to copy the new properties during C loning operations. " > when deriving from DataGridViewCell or DataGridViewColumn and adding new attributes to the derived class, be sure to override Clone method to copy the new properties during the cloning operation. Clone method to copy the properties of the base class to the new cell or column. |
Example c#vb
Using system;using system.drawing;using system.windows.forms;using system.windows.forms.visualstyles;class Form1: form{private DataGridView DataGridView1 = new DataGridView (); [STAThread] public static void Main () {application.enablevisualstyles (); Application.Run (New Form1 ()); } public Form1 () {this. AutoSize = true; This. Load + = new EventHandler (Form1_Load); } public void Form1_Load (object sender, EventArgs e) {datagridviewcheckboxcolumn column0 = new Da Tagridviewcheckboxcolumn (); Datagridviewdisablebuttoncolumn column1 = new Datagridviewdisablebuttoncolumn (); Column0. Name = "checkboxes"; Column1. Name = "Buttons"; DATAGRIDVIEW1.COLUMNS.ADD (Column0); DATAGRIDVIEW1.COLUMNS.ADD (Column1); Datagridview1.rowcount = 8; Datagridview1.autosize = true; Datagridview1.allowusertoaddrows = false; DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = datagridviewcontentalignment.middlecenter; Set the text for each button. for (int i = 0; i < Datagridview1.rowcount; i++) {datagridview1.rows[i]. cells["Buttons"]. Value = "button" + i.tostring (); } datagridview1.cellvaluechanged + = new Datagridviewcelleventhandler (datagridview1_cellvaluechanged); Datagridview1.currentcelldirtystatechanged + = new EventHandler (datagridview1_currentcelldirtystatechanged ); Datagridview1.cellclick + = new Datagridviewcelleventhandler (Datagridview1_cellclick); This. Controls.Add (DATAGRIDVIEW1); }//This event handler manually raises the Cellvaluechanged event/by calling the Commitedit method. void Datagridview1_currentcelldirtystatechanged (object sender, EventArgs e) {if (datagridview1.iscurrent Celldirty) {datagridview1.commitedit (datagridviewdataerrorcontexts.commit); }}//If A check box cell is clicked, this event handler disables//or enables the button in the same R ow as the clicked cell. public void Datagridview1_cellvaluechanged (object sender, DataGridViewCellEventArgs e) {if (DataGridView 1.columns[e.columnindex]. Name = = "checkboxes") {DataGridViewDisableButtonCell Buttoncell = (datagridviewdisablebu Ttoncell) DataGridView1. Rows[e.rowindex]. cells["Buttons"]; Datagridviewcheckboxcell Checkcell = (Datagridviewcheckboxcell) dataGridView1. Rows[e.rowindex]. cells["checkboxes"]; buttoncell.enabled =! (Boolean) Checkcell.value; Datagridview1.invalidate (); }}//If the user clicks on a enabled button cell, this event handler//Reports the button is enabled. void Datagridview1_cellclick (object sender, DataGridViewCellEventArgs e) {if (datagridview1.columns[e . COlumnindex]. Name = = "Buttons") {DataGridViewDisableButtonCell Buttoncell = (datagridviewdisablebutto NCell) DataGridView1. Rows[e.rowindex]. cells["Buttons"]; if (buttoncell.enabled) {MessageBox.Show (Datagridview1.rows[e.rowindex]. Cells[e.columnindex]. Value.tostring () + "is enabled"); }}}}public class datagridviewdisablebuttoncolumn:datagridviewbuttoncolumn{public Datagridviewdisablebutt Oncolumn () {this. CellTemplate = new DataGridViewDisableButtonCell (); }}public class datagridviewdisablebuttoncell:datagridviewbuttoncell{private bool Enabledvalue; public bool Enabled {get {return enabledvalue; } set {enabledvalue = value; }}//Override the Clone method So, the the Enabled property is copied. public override Object Clone () { DataGridViewDisableButtonCell cell = (DataGridViewDisableButtonCell) base. Clone (); Cell. Enabled = this. Enabled; return cell; }//By default, enable the button cell. Public DataGridViewDisableButtonCell () {this.enabledvalue = true; } protected override void Paint (graphics graphics, Rectangle clipbounds, Rectangle cellbounds, int rowIndex, DataGridViewElementStates Elementstate, Object value, Object FormattedValue, String errorText, Datagridv Iewcellstyle CellStyle, DataGridViewAdvancedBorderStyle advancedborderstyle, Datagridviewpaintparts PaintPar TS) {//the button cell is disabled, so paint the border,//background, and disabled button for the C Ell. if (!this.enabledvalue) {//Draw the cell background, if specified. if ((Paintparts & datagridviewpaintparts.background) = = Datagridviewpaintparts.background) { SolidBrush cellbackground = new SolidBrush (Cellstyle.backcolor); Graphics. FillRectangle (Cellbackground, cellbounds); Cellbackground.dispose (); }//Draw the cell borders, if specified. if ((Paintparts & datagridviewpaintparts.border) = = Datagridviewpaintparts.border) { Paintborder (Graphics, clipbounds, Cellbounds, CellStyle, Advancedborderstyle); }//Calculate the which to draw the button. Rectangle Buttonarea = cellbounds; Rectangle buttonadjustment = this. Borderwidths (Advancedborderstyle); buttonarea.x + = Buttonadjustment.x; Buttonarea.y + = Buttonadjustment.y; Buttonarea.height-= Buttonadjustment.height; Buttonarea.width-= Buttonadjustment.width; Draw the disabled button.Buttonrenderer.drawbutton (Graphics, Buttonarea, pushbuttonstate.disabled); Draw the Disabled button text. if (this. FormattedValue is a String) {Textrenderer.drawtext (graphics, (String) this. FormattedValue, this. Datagridview.font, Buttonarea, Systemcolors.graytext); }} else {//the button cell is enabled, so let the base class//Handle the Painting. Base. Paint (Graphics, clipbounds, Cellbounds, RowIndex, elementstate, value, FormattedValue, ErrorText, CellStyle, Advancedborderstyle, paintparts); } }}
How to: Disable a button in the button column of a Windows forms DataGridView control