我有一个TabControl,每个TabPage上都有一个DataGridView。DataGridView列中有一个DataGridViewCheckBoxCell。
我想取消选中位于所有DataGridViewCheckBoxes的DataGridView的同一行上的TabPages。
我只能访问单击的DataGridView上的TabPage。看起来,来自myDataGrid_CellContentClick事件的发送方对象不包含其他TabPages。
如何将checkBox设置在另一个TabPages上。
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;
}
}
}
}
}发布于 2015-07-12 08:47:41
如果每个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;
}
}
}
}需要注意的是,这段代码假设每个页面只有一个网格,并且每个网格包含的行数与单击的网格相同。
https://stackoverflow.com/questions/31365983
复制相似问题