博客分类:
DataGridView 控件包括 DataGridViewButtonCell 类,该类用于显示具有类似按钮的用户界面 (UI) 的单元格。但 DataGridViewButtonCell 不提供禁用由单元格显示的按钮外观的方式。 下面的代码示例演示如何自定义 DataGridViewButtonCell 类来显示可以显示为禁用的按钮。本示例定义一个新的单元格类型 DataGridViewDisableButtonCell,它由 DataGridViewButtonCell 派生。此单元格类型提供一个新的 Enabled 属性,可以将该属性设置为 false 来在单元格中绘制禁用的按钮。本示例还定义一个新的列类型 DataGridViewDisableButtonColumn,它显示 DataGridViewDisableButtonCell 对象。为了演示此新单元格类型和列类型,父 DataGridView 中的每个 DataGridViewCheckBoxCell 的当前值确定同一行中 DataGridViewDisableButtonCell 的 Enabled 属性是 true 还是 false。 首先要在代码中加入下面两个继承类: datagridview的button控件要在代码里面加入: 如加入编辑按钮: 禁用:
- public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn
- {
- public DataGridViewDisableButtonColumn()
- {
- this.CellTemplate = new DataGridViewDisableButtonCell();
- }
- }
- public class DataGridViewDisableButtonCell : DataGridViewButtonCell
- {
- private bool enabledValue;
- public bool Enabled
- {
- get
- {
- return enabledValue;
- }
- set
- {
- enabledValue = value;
- }
- }
- public override object Clone()
- {
- DataGridViewDisableButtonCell cell =
- (DataGridViewDisableButtonCell)base.Clone();
- cell.Enabled = this.Enabled;
- return 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,
- DataGridViewCellStyle cellStyle,
- DataGridViewAdvancedBorderStyle advancedBorderStyle,
- DataGridViewPaintParts paintParts)
- {
- if (!this.enabledValue)
- {
- if ((paintParts & DataGridViewPaintParts.Background) ==
- DataGridViewPaintParts.Background)
- {
- SolidBrush cellBackground =
- new SolidBrush(cellStyle.BackColor);
- graphics.FillRectangle(cellBackground, cellBounds);
- cellBackground.Dispose();
- }
- if ((paintParts & DataGridViewPaintParts.Border) ==
- DataGridViewPaintParts.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);
- }
- }
- }
- 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);
- DataGridViewDisableButtonCell buttonCell = (DataGridViewDisableButtonCell)dgv_Promotiom.Rows[i].Cells["btn_ProEdit"];
- buttonCell.Enabled = false;