1

I have a TabControl with on each TabPage a DataGridView. The DataGridView has in Column[0] a DataGridViewCheckBoxCell.

I want to uncheck the DataGridViewCheckBoxes on the same Row of the DataGridView of all the TabPages.

I can only access the DataGridView on the clicked TabPage. It looks like the sender object from the myDataGrid_CellContentClick event doesn't contain the other TabPages.

How can I set the checkBox on the other 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;
                    }

                }
            }
        }

    }
Jan-WIllem
  • 91
  • 13

1 Answers1

0

If every TabPage contains a different DataGrid then you need to reference the appropriate grid, select the matching row and check the cell

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

It is very important to note that this code assumes that you have only one grid per page and every grid contains the same amount of rows as the one clicked.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thanks for your reply, The DataGrid class doesn't contain a property called Row? – Jan-WIllem Jul 12 '15 at 09:34
  • Thanks , I'm veryy sorry but also Rows isn't a vallid property? Error 1 'System.Windows.Forms.DataGrid' does not contain a definition for 'Rows' and no extension method 'Rows' accepting a first argument of type 'System.Windows.Forms.DataGrid' could be found (are you missing a using directive or an assembly reference?) – Jan-WIllem Jul 12 '15 at 10:25
  • 1
    Super, Yes thats it. But also the fist Datagrid must change to DataGridView. I tried to edit but get an error. Can you change it? – Jan-WIllem Jul 12 '15 at 11:03