![](/img/trans.png)
[英]How to exclusively set the value of a DataGridViewCheckBoxCell?
[英]Set DataGridViewCheckBoxCell on all TabPage tabs
我有一个TabControl,在每个TabPage上都有一个DataGridView。 DataGridView在列[0]中有一个DataGridViewCheckBoxCell。
我想取消选中所有TabPage的DataGridView的同一行上的DataGridViewCheckBoxes。
我只能在单击的TabPage上访问DataGridView。 看起来myDataGrid_CellContentClick事件中的发件人对象不包含其他TabPage。
如何在其他TabPage上设置复选框。
void myDataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int clickedRow = e.RowIndex;
int clickedColumn = e.ColumnIndex;
if (clickedColumn != 0) return;
DataGridView myDataGridView = (DataGridView)sender;
if (!ToggleAllRowSelection)
{
foreach (TabPage myTabPage in tabControl1.TabPages)
{
foreach (DataGridViewRow myRow in myDataGridView.Rows)
{
if (myRow.Index == clickedRow)
{
((DataGridViewCheckBoxCell)myRow.Cells[0]).Value = false;
}
}
}
}
}
如果每个TabPage都包含一个不同的DataGrid,则您需要引用适当的网格,选择匹配的行并检查单元格
void myDataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int clickedRow = e.RowIndex;
int clickedColumn = e.ColumnIndex;
if (clickedColumn != 0) return;
DataGridView myDataGridView = (DataGridView)sender;
if (!ToggleAllRowSelection)
{
foreach (TabPage myTabPage in tabControl1.TabPages)
{
DataGridView grd = myTabPage.Controls.OfType<DataGridView>().FirstOrDefault();
if(grd != null)
{
grd.Rows[clickedRow].Cells[0].Value = false;
}
}
}
}
请务必注意,此代码假设您每页只有一个网格,并且每个网格包含的行数与单击的行数相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.