这个问题折腾很久了,网上找了半天了,结果问一个同事,同事给了一个地址让我看看,最后根据上面的介绍做好了,再次记录一下,o(∩_∩)o 哈哈
地址如下:
代码如下:
1 public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn 2 { 3 public DataGridViewDisableButtonColumn() 4 { 5 this.CellTemplate = new DataGridViewDisableButtonCell(); 6 } 7 } 8 9 10 public class DataGridViewDisableButtonCell : DataGridViewButtonCell11 {12 private bool enabledValue;13 14 public bool Enabled15 {16 get17 {18 return enabledValue;19 }20 set21 {22 enabledValue = value;23 }24 }25 26 public string CommadText { get; set; }27 28 // Override the Clone method so that the Enabled property is copied. 29 public override object Clone()30 {31 DataGridViewDisableButtonCell cell = (DataGridViewDisableButtonCell)base.Clone();32 cell.Enabled = this.Enabled;33 return cell;34 }35 36 // By default, enable the button cell. 37 public DataGridViewDisableButtonCell()38 {39 this.enabledValue = true;40 }41 42 protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,43 DataGridViewElementStates elementState, object value,44 object formattedValue, string errorText, DataGridViewCellStyle cellStyle,45 DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)46 {47 48 if (!this.enabledValue)49 {50 // Draw the cell background, if specified. 51 if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)52 {53 SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);54 graphics.FillRectangle(cellBackground, cellBounds);55 cellBackground.Dispose();56 }57 58 // Draw the cell borders, if specified. 59 if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)60 {61 PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);62 }63 64 // Calculate the area in which to draw the button.65 Rectangle buttonArea = cellBounds;66 Rectangle buttonAdjustment = this.BorderWidths(advancedBorderStyle);67 buttonArea.X += buttonAdjustment.X;68 buttonArea.Y += buttonAdjustment.Y;69 buttonArea.Height -= buttonAdjustment.Height;70 buttonArea.Width -= buttonAdjustment.Width;71 72 // Draw the disabled button. 73 ButtonRenderer.DrawButton(graphics, buttonArea, PushButtonState.Disabled);74 75 // Draw the disabled button text. 76 if (this.FormattedValue is String)77 {78 TextRenderer.DrawText(graphics, (string)this.FormattedValue, this.DataGridView.Font, buttonArea, SystemColors.GrayText);79 }80 }81 else82 {83 // The button cell is enabled, so let the base class 84 // handle the painting. 85 base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);86 }87 }88 }
在此记录一下!
已同步至: