首页 > 代码库 > 如何:禁用 Windows 窗体 DataGridView 控件的按钮列中的按钮

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

转自:https://msdn.microsoft.com/zh-cn/library/ms171619(v=vs.110).aspx

 

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

以下代码示例演示如何自定义 DataGridViewButtonCell 类以显示可以显示为禁用的按钮。 此示例定义了一个新的单元格类型,即从 DataGridViewButtonCell 派生的 DataGridViewDisableButtonCell。 此单元格类型提供了一种新 Enabled 属性,可设置为 false 以拖动单元格中已禁用的按钮。 此示例还定义了一个新的列类型,即显示 DataGridViewDisableButtonCell 对象的DataGridViewDisableButtonColumn。 为了演示此新单元格和列类型,父级 DataGridView 中的每个DataGridViewCheckBoxCell 的当前值确定了相同行中 DataGridViewDisableButtonCell 的 Enabled 属性是否是 true 或false

技术分享注意

当从 DataGridViewCell 或 DataGridViewColumn 进行派生并将新属性添加到派生的类时,请确保重写 Clone 方法以在克隆操作过程中复制新属性。 还应调用基类的 Clone 方法,以便将基类的属性复制到新的单元格或列。

示例

C#
VB
 
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 =                "http://www.mamicode.com/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 = http://www.mamicode.com/value;>

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