The datagridview control includes the datagridviewbuttoncell class, which is used to display cells with user interfaces (UIS) similar to buttons. However, datagridviewbuttoncell does not disable the appearance of buttons displayed by cells.
The followingCodeThe following example shows 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 datagridviewbuttoncell. This cell type provides a new enabled attribute, which can be set to false to draw a disabled button in the cell. This example also defines a new column type: datagridviewdisablebuttoncolumn, which displays the datagridviewdisablebuttoncell object. To demonstrate the new cell type and column type, the current value of each datagridviewcheckboxcell in the parent dview determines whether the enabled attribute of the datagridviewdisablebuttoncell in the same row is true or false.
First, add the following two inheritance classes to the Code:
Public class datagridviewdisablebuttoncolumn: datagridviewbuttoncolumn {public datagridviewdisablebuttoncolumn () {This. celltemplate = new inline () ;}} public class maid: Maid {private bool enabledvalue; Public bool enabled {get {return enabledvalue;} set {enabledvalue = value ;}} public override object clone () {datagridviewdisablebuttoncell Cell = (Maid) base. Clone (); Cell . Enabled = This. enabled; Return Cell ;} Public datagridviewdisablebuttoncell () {This. protected = true;} protected override void paint (Graphics graphics, rectangle finished, rectangle cellbounds, int rowindex, datagridviewelementstates elementstate, object value, object success, string errortext, datagridviewcellstyle cellstyle, datagridviewadvancedborderstyle cedborderstyle, datagridviewpaintparts paintparts) {if (! This. enabledvalue) {If (paintparts & datagridviewpaintparts. background) = maid. background) {solidbrush cellbackground = new solidbrush (cellstyle. backcolor); graphics. fillrectangle (cellbackground, cellbounds); cellbackground. dispose ();} If (paintparts & datagridviewpaintparts. border) = maid. border) {paintborder (graphics, clipbounds, cellbounds, cellstyle, advancedborderstyle);} rectangle buttonarea = cellbounds; rectangle buttonadjustment = This. borderwidths (advancedborderstyle); buttonarea. X + = buttonadjustment. x; buttonarea. Y + = buttonadjustment. y; buttonarea. height-= buttonadjustment. height; buttonarea. width-= buttonadjustment. width; buttonrenderer. drawbutton (graphics, buttonarea, system. windows. forms. visualstyles. pushbuttonstate. disabled); If (this. formattedvalue is string) {textrenderer. drawtext (graphics, (string) This. formattedvalue, this. datagridview. font, buttonarea, systemcolors. graytext) ;}} else {base. paint (graphics, clipbounds, cellbounds, rowindex, elementstate, value, formattedvalue, errortext, cellstyle, advancedborderstyle, paintparts );}}}
TheButtonAdd the following controls to the Code:
For example, add the edit button:
Datagridviewdisablebuttoncolumn btn_proedit = new datagridviewdisablebuttoncolumn (); btn_proedit.headertext = L. alanuagebinding ("gv_action"); btn_proedit.text = L. alanuagebinding ("gv_edit"); btn_proedit.name = "btn_proedit"; btn_proedit.width = 50; btn_proedit.usecolumntextforbuttonvalue = true; this. datagridview1.columns. add (btn_proedit );
Disable:
Datagridviewdisablebuttoncell buttoncell = (datagridviewdisablebuttoncell) dgv_promotiom.rows [I]. cells ["btn_proedit"]; buttoncell. Enabled = false;