禁用 Windows 窗体 DataGridView 控件的按钮列中的按钮

//from :http://www.5acs.com/article.asp?id=472

DataGridView 控件包括 DataGridViewButtonCell 类,该类用于显示具有类似按钮的用户界面 (UI) 的单元格。但 DataGridViewButtonCell 不提供禁用由单元格显示的按钮外观的方式。  下面的代码示例演示如何自定义 DataGridViewButtonCell 类来显示可以显示为禁用的按钮。本示例定义一个新的单元格类型 DataGridViewDisableButtonCell,它由 DataGridViewButtonCell 派生。此单元格类型提供一个新的 Enabled 属性,可以将该属性设置为 false 来在单元格中绘制禁用的按钮。本示例还定义一个新的列类型 DataGridViewDisableButtonColumn,它显示 DataGridViewDisableButtonCell 对象。为了演示此新单元格类型和列类型,父 DataGridView 中的每个 DataGridViewCheckBoxCell 的当前值确定同一行中 DataGridViewDisableButtonCell 的 Enabled 属性是 true 还是 false。 注意  当从 DataGridViewCell 或 DataGridViewColumn 派生并向派生类添加新属性时,请确保重写 Clone 方法以便在克隆操作期间复制新属性。还应调用基类的 Clone 方法,以便将基类的属性复制到新的单元格或列中。   示例 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 DataGridViewCheckBoxColumn();         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.IsCurrentCellDirty)         {             dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);         }     }     // If a check box cell is clicked, this event handler disables       // or enables the button in the same row as the clicked cell.     public void dataGridView1_CellValueChanged(object sender,         DataGridViewCellEventArgs e)     {         if (dataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxes")         {             DataGridViewDisableButtonCell buttonCell =                 (DataGridViewDisableButtonCell)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 an enabled button cell, this event handler       // reports that the button is enabled.     void dataGridView1_CellClick(object sender,         DataGridViewCellEventArgs e)     {         if (dataGridView1.Columns[e.ColumnIndex].Name == "Buttons")         {             DataGridViewDisableButtonCell buttonCell =                 (DataGridViewDisableButtonCell)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 DataGridViewDisableButtonColumn()     {         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 that 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,         DataGridViewCellStyle cellStyle,         DataGridViewAdvancedBorderStyle advancedBorderStyle,         DataGridViewPaintParts paintParts)     {         // The button cell is disabled, so paint the border,           // background, and disabled button for the cell.         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 area in 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 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);         }     } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值