可别小看了这个问题。我试了很多的datagridview事件,比如 cellClick CellContentChanged 等事件,要不就是无法扑捉,要不就是得到的值为选中前的值:false 。
当然,最后无意找到了正确的事件:cellContentClick 事件。在该事件中可得知选中前后的值,代码如下:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 3 && Convert.ToInt32(tbCpiName.Tag)>0 )//当单击复选框,同时处于组合编辑状态时
{
DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
bool ifcheck1 = Convert.ToBoolean(cell.FormattedValue);
bool ifcheck2 = Convert.ToBoolean(cell.EditedFormattedValue);
if (ifcheck1 != ifcheck2)
{
BCItem bcitem = new BCItem();
bcitem.B_bciID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["bciID"].Value);
bcitem.C_CpiID = Convert.ToInt32(tbCpiName.Tag);
if (ifcheck2)//将该项目与组合关联
{
bllcp.AddCompageAndItem(bcitem);//关联
}
else
{
bllcp.DeleteCompageAndItem(bcitem);//取消关联
}
}
}
}
DataGridView Checkbox 事件
本文介绍在 WinForms 中使用 DataGridView 控件时,如何正确地监听 CheckBox 列的状态改变,并执行相应操作。通过使用 cellContentClick 事件,可以在 CheckBox 的状态发生变化时及时捕获并做出响应。
791

被折叠的 条评论
为什么被折叠?



