如何为动态创建的按钮列创建按钮单击功能。
DataGridViewButtonColumn select = new DataGridViewButtonColumn();
select.Text = "Details";
select.HeaderText = "Details";
select.Name = "Select";
dataGridView5.Columns.Add(select);
这是我在datagridview中动态创建按钮的代码。
您能举例说明如何为onclick按钮编写button_click
函数。
答案 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));
}
}
答案 1 :(得分:1)
您可以处理DataGridView.CellClick
或DataGridView.CellContentClick
事件。
答案 2 :(得分:0)