I want to fire an event when any of the datagridviewcheckbox is checked. I tried the foreach but it only trigger when all the datagridviewcheck is checked. I want to fire an event if any of the datagridviewcheckboxcell is checked.
foreach (DataGridViewRow row in dgvLocal.Rows)
{
if ((Convert.ToBoolean(row.Cells[0].Value) == true))
{
//
}
}
Use the cellcontentclicked event of the datagridview Also use the CurrentCellDirtystateChanged to make sure the last click is commited
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)
}
}
}
Try this out
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)row.Cells[1];
if (check.Value == check.TrueValue)
{
//dosomething
}
else
{
//dosomething
}
}
Trigger 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
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.