c# - C#如何检查是否选中了任何datagridview复选框
问题描述
我想在选中任何 datagridviewcheckbox 时触发一个事件。我尝试了 foreach 但它仅在检查所有 datagridviewcheck 时触发。如果选中任何datagridviewcheckboxcell ,我想触发一个事件。
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)
}
}
}
推荐阅读
- google-analytics - jQuery 获取 cookie 的值并发送到 GA
- javascript - Angular如何动态更改数组中的值
- r - 采购数据框并循环创建列表/数据框
- python - 将 Dataframe.describe 输出转换为一些 json
- dataframe - 如何在数据框选择中添加中间列?
- android - Android Espresso:由于随机 ViewNotFoundException 导致的不稳定测试
- c++ - 为什么直接 x 11 中我的程序没有输出?
- python - 如何模拟 micrologix Allen Bradley PLC?
- c# - 如何将列表视图中的 OberservableCollection 列表从 api 查询项复制到新的列表视图
- css - Flex Column Wrapping 和 Flex Basis 100% 额外空间