在所有TabPage选项卡上设置DataGridViewCheckBoxCell

时间:2015-07-12 08:43:07

标签: c# tabcontrol datagridviewcheckboxcell

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

我想在所有TabPages的DataGridView的同一行上取消选中DataGridViewCheckBox。

我只能在点击的TabPage上访问DataGridView。看起来myDataGrid_CellContentClick事件中的sender对象不包含其他TabPages。

如何在其他TabPages上设置checkBox。

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

                }
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

如果每个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;
            }
        }
    }
}

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