在所有 TabPage 选项卡上设置 DataGridViewCheckBoxCell

Set DataGridViewCheckBoxCell on all TabPage tabs

我有一个 TabControl,每个 TabPage 上都有一个 DataGridView。 DataGridView 在 Column[0] 中有一个 DataGridViewCheckBoxCell。

我想取消选中所有 TabPage 的 DataGridView 的同一行上的 DataGridViewCheckBoxes。

我只能访问点击的TabPage 上的DataGridView。 myDataGrid_CellContentClick 事件中的发件人对象似乎不包含其他 TabPages。

如何在其他 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;
                    }

                }
            }
        }

    }

如果每个 TabPage 包含不同的 DataGrid,那么您需要引用适当的网格,select 匹配行并检查单元格

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;
            }
        }
    }
}

请务必注意,此代码假定您每页只有一个网格,并且每个网格包含的行数与单击的行数相同。