我想在选中任何datagridview复选框时触发一个事件。我尝试了foreach,但只有在检查了所有datagridviewcheck后才会触发。 如果选中了datagridviewcheckboxcell的任何,我想触发一个事件。
foreach (DataGridViewRow row in dgvLocal.Rows)
{
if ((Convert.ToBoolean(row.Cells[0].Value) == true))
{
//
}
}
答案 0 :(得分:1)
使用datagridview的cellcontentclicked事件 另外,使用CurrentCellDirtystateChanged确保最后一次点击已提交
void grd_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (grd.IsCurrentCellDirty)
grd.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void grd_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1) //compare to checkBox column index
{
DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)grd[e.ColumnIndex, e.RowIndex];
if (!DBNull.Value.Equals(cbx.Value) && (bool)cbx.Value == true)
{
//checkBox is checked - do the code in here!
}
else
{
//if checkBox is NOT checked (unchecked)
}
}
}
答案 1 :(得分:0)
尝试一下
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)row.Cells[1];
if (check.Value == check.TrueValue)
{
//dosomething
}
else
{
//dosomething
}
}
答案 2 :(得分:0)
触发CellValueChanged
private void dgvProducts_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dgvProducts.DataSource != null)
{
if (dgvProducts.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "True")
{
//do something
}
else
{
//do something
}
}
}