DataGridView 控件包括 DataGridViewButtonCell 类,该类用于显示具有类似按钮的用户界面 (UI) 的单元格。但 DataGridViewButtonCell 不提供禁用由单元格显示的按钮外观的方式。 

下面的代码示例演示如何自定义 DataGridViewButtonCell 类来显示可以显示为禁用的按钮。本示例定义一个新的单元格类型 DataGridViewDisableButtonCell,它由 DataGridViewButtonCell 派生。此单元格类型提供一个新的 Enabled 属性,可以将该属性设置为 false 来在单元格中绘制禁用的按钮。本示例还定义一个新的列类型 DataGridViewDisableButtonColumn,它显示 DataGridViewDisableButtonCell 对象。为了演示此新单元格类型和列类型,父 DataGridView 中的每个 DataGridViewCheckBoxCell 的当前值确定同一行中 DataGridViewDisableButtonCell 的 Enabled 属性是 true 还是 false。 

首先要在代码中加入下面两个继承类: 
C#代码   收藏代码
  1. public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn  
  2.     {  
  3.         public DataGridViewDisableButtonColumn()  
  4.         {  
  5.             this.CellTemplate = new DataGridViewDisableButtonCell();  
  6.         }  
  7.     }  
  8.   
  9.     public class DataGridViewDisableButtonCell : DataGridViewButtonCell  
  10.     {  
  11.         private bool enabledValue;  
  12.         public bool Enabled  
  13.         {  
  14.             get  
  15.             {  
  16.                 return enabledValue;  
  17.             }  
  18.             set  
  19.             {  
  20.                 enabledValue = value;  
  21.             }  
  22.         }  
  23.   
  24.         public override object Clone()  
  25.         {  
  26.             DataGridViewDisableButtonCell cell =  
  27.                 (DataGridViewDisableButtonCell)base.Clone();  
  28.             cell.Enabled = this.Enabled;  
  29.             return cell;  
  30.         }  
  31.   
  32.         public DataGridViewDisableButtonCell()  
  33.         {  
  34.             this.enabledValue = true;  
  35.         }  
  36.   
  37.         protected override void Paint(Graphics graphics,  
  38.             Rectangle clipBounds, Rectangle cellBounds, int rowIndex,  
  39.             DataGridViewElementStates elementState, object value,  
  40.             object formattedValue, string errorText,  
  41.             DataGridViewCellStyle cellStyle,  
  42.             DataGridViewAdvancedBorderStyle advancedBorderStyle,  
  43.             DataGridViewPaintParts paintParts)  
  44.         {  
  45.             if (!this.enabledValue)  
  46.             {  
  47.                 if ((paintParts & DataGridViewPaintParts.Background) ==  
  48.                     DataGridViewPaintParts.Background)  
  49.                 {  
  50.                     SolidBrush cellBackground =  
  51.                         new SolidBrush(cellStyle.BackColor);  
  52.                     graphics.FillRectangle(cellBackground, cellBounds);  
  53.                     cellBackground.Dispose();  
  54.                 }  
  55.   
  56.                 if ((paintParts & DataGridViewPaintParts.Border) ==  
  57.                     DataGridViewPaintParts.Border)  
  58.                 {  
  59.                     PaintBorder(graphics, clipBounds, cellBounds, cellStyle,  
  60.                         advancedBorderStyle);  
  61.                 }  
  62.                 Rectangle buttonArea = cellBounds;  
  63.                 Rectangle buttonAdjustment =  
  64.                     this.BorderWidths(advancedBorderStyle);  
  65.                 buttonArea.X += buttonAdjustment.X;  
  66.                 buttonArea.Y += buttonAdjustment.Y;  
  67.                 buttonArea.Height -= buttonAdjustment.Height;  
  68.                 buttonArea.Width -= buttonAdjustment.Width;  
  69.                 ButtonRenderer.DrawButton(graphics, buttonArea,  
  70.                     System.Windows.Forms.VisualStyles.PushButtonState.Disabled);  
  71.   
  72.                 if (this.FormattedValue is String)  
  73.                 {  
  74.                     TextRenderer.DrawText(graphics,  
  75.                         (string)this.FormattedValue,  
  76.                         this.DataGridView.Font,  
  77.                         buttonArea, SystemColors.GrayText);  
  78.                 }  
  79.             }  
  80.             else  
  81.             {  
  82.                 base.Paint(graphics, clipBounds, cellBounds, rowIndex,  
  83.                     elementState, value, formattedValue, errorText,  
  84.                     cellStyle, advancedBorderStyle, paintParts);  
  85.             }  
  86.         }  
  87.     }  


datagridview的button控件要在代码里面加入: 
如加入编辑按钮: 
C#代码   收藏代码
  1. DataGridViewDisableButtonColumn btn_ProEdit = new DataGridViewDisableButtonColumn();  
  2.             btn_ProEdit.HeaderText = l.ALanuageBinding("gv_Action");  
  3.             btn_ProEdit.Text = l.ALanuageBinding("gv_Edit");  
  4.             btn_ProEdit.Name = "btn_ProEdit";  
  5.             btn_ProEdit.Width = 50;  
  6.             btn_ProEdit.UseColumnTextForButtonValue = true;  
  7. this.datagridview1.Columns.Add(btn_ProEdit);  


禁用: 
C#代码   收藏代码
  1. DataGridViewDisableButtonCell buttonCell = (DataGridViewDisableButtonCell)dgv_Promotiom.Rows[i].Cells["btn_ProEdit"];  
  2.   
  3.                         buttonCell.Enabled = false;