C#如何检查是否选中了任何datagridview复选框
我想在选中任何datagridviewcheckbox时触发事件。我尝试了foreach,但它仅在选中所有datagridviewcheck时触发。 如果选中datagridviewcheckboxcell的任何,我想触发一个事件C#如何检查是否选中了任何datagridview复选框,c#,C#,我想在选中任何datagridviewcheckbox时触发事件。我尝试了foreach,但它仅在选中所有datagridviewcheck时触发。 如果选中datagridviewcheckboxcell的任何,我想触发一个事件 foreach (DataGridViewRow row in dgvLocal.Rows) { if ((Convert.ToBoolean(row.Cells[0].Value) == true)) { // }
foreach (DataGridViewRow row in dgvLocal.Rows)
{
if ((Convert.ToBoolean(row.Cells[0].Value) == true))
{
//
}
}
使用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)
}
}
}
试试这个
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)row.Cells[1];
if (check.Value == check.TrueValue)
{
//dosomething
}
else
{
//dosomething
}
}
触发器单元值已更改
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
}
}
}
什么是“触发”这个foreach?我认为我们这里缺少了一些上下文。该代码看起来在找到第一个检查的项目时会起作用,您只需要在
if
中使用中断
,这样它在找到一个项目时就会停止检查。如果你有,那么肯定还有其他原因。我不知道我将在哪个事件上执行此代码。我只想在检查datagridview中的任何checkboxcell时显示一个messagebox。错误5运算符“==”不能应用于“object”和“bool”类型的操作数C:\Users\MOMO\Desktop\MLQ System 9.26\MLQ SYTSTEM 9.26\MLQSystem\UpdateVisitors。cs 924 21 MLQSystemtry使用“=”而不是“==”