WinForm中 dataGridView中的按钮绑定单击事件

DataGridView单元格增加按钮,单击事件

public DataGridViewFrm()
{
InitializeComponent();
DataGridViewButtonColumn btn1 = new DataGridViewButtonColumn();
btn1.HeaderText = "修改";
btn1.Text = "修改";
btn1.Name = "edit";
btn1.Width = 60;
btn1.UseColumnTextForButtonValue = true;
this.dgvShow.Columns.Add(btn1);
dgvShow.Columns[3].HeaderText = "姓名";
this.dgvShow.CellFormatting += new DataGridViewCellFormattingEventHandler(dgvShow_CellFormatting);
}
///


/// 单元格单击事件,不是CellClick
///

///
///
private void dgvShow_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dgvShow.Columns[e.ColumnIndex].Name == "edit")
{
//可以在此打开新窗口,把参数传递过去
MessageBox.Show(this.dgvShow.Rows[e.RowIndex].Cells[3].Value.ToString());
}
}

在datagridview中单击按钮事件
private void ThylxBtn_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

int CIndex = e.ColumnIndex; //获取当前列的索引

if (CIndex == 3) 是用第0列开始记的,所以这里的3指的是第4列

{

messageBox.show("OK!")

}

}

你可能感兴趣的:(C#)