如何在datagridview中为动态创建的按钮列按钮_单击功能

时间:2012-08-01 12:10:42

标签: c# c#-4.0 datagridview

如何为动态创建的按钮列创建按钮单击功能。

DataGridViewButtonColumn select = new DataGridViewButtonColumn();
select.Text = "Details";
select.HeaderText = "Details";
select.Name = "Select";
dataGridView5.Columns.Add(select);

这是我在datagridview中动态创建按钮的代码。

您能举例说明如何为onclick按钮编写button_click函数。

3 个答案:

答案 0 :(得分:2)

使用CellContentClick-Event

示例:

 private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.CellContentClick += new DataGridViewCellEventHandler(dataGridView1_CellContentClick);

        DataGridViewButtonColumn select = new DataGridViewButtonColumn();
        select.Text = "Details";
        select.HeaderText = "Details";
        select.Name = "Select";
        dataGridView1.Columns.Add(select);
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == dataGridView1.Columns["Select"].Index)
        {
            MessageBox.Show(String.Format("Clicked! Row: {0}", e.RowIndex));
        }
    }

CellContentClick on MSDN

答案 1 :(得分:1)

您可以处理DataGridView.CellClickDataGridView.CellContentClick事件。

答案 2 :(得分:0)