0

I have 2 DataGridView controls on a form. Both have same number and types of columns. 1 TextBox Column and 2 CheckBoxColumns. The Problem is that first DataGridView is working fine but the other one is not. both have same binding methods and datasource. The problems with the second DataGridView on the same form are..

  • Checkbox values are not set

  • currentrow.Tag value is null when I try to retrieve the value

Below is the code i'm using to bind the DataGridViews and setting checkbox values

public void BindGridView(DataGridView gv)
    {
        var actuallist = UserOperations.GetPermissions(RoleId, (int)(Enumerations.ModuleType.Basic));
        Common.Common.StyleGridView(gv);
        gv.AutoGenerateColumns = false;

        gv.Columns["ModuleName"].DataPropertyName = "ModuleName";

        gv.DataSource = actuallist;

        int j = 0;
        foreach (DataGridViewRow row in gv.Rows)
        {
            row.Tag = actuallist[j++].ModuleId;
        }
        int k = 0;
        bool r = false;
        foreach (DataGridViewRow row in gv.Rows)
        {
            r = actuallist[k++].PermissionGranted;
            if (r)
                ((DataGridViewCheckBoxCell)row.Cells[1]).Value = r;
            else
                ((DataGridViewCheckBoxCell)row.Cells[2]).Value = !r;
        }
    }

 private void gvPermissions_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (gvPermissions.Columns[e.ColumnIndex].Name == "Granted")
        {
            bool isChecked = (bool)gvPermissions[e.ColumnIndex, e.RowIndex].EditedFormattedValue;
            gvPermissions.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = !isChecked;
            gvPermissions.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value = isChecked;
            gvPermissions.EndEdit();
        }

        if (gvPermissions.Columns[e.ColumnIndex].Name == "Denied")
        {
            bool isChecked = (bool)gvPermissions[e.ColumnIndex, e.RowIndex].EditedFormattedValue;
            gvPermissions.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = !isChecked;
            gvPermissions.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value = isChecked;
            gvPermissions.EndEdit();
        }
    }
THunter
  • 323
  • 5
  • 11
  • step thru the code.. also what is r = `actuallist[k++].PermissionGranted;` supposed to represent..? is it supposed to increment..? – MethodMan Dec 22 '14 at 23:53
  • @DJKRAZE PermissionGranted is bool. its from the list which is also used as datasource for the DataGridview. from this value, the next part determines which checkbox row should be checked. – THunter Dec 23 '14 at 00:13
  • Can you try to call the BindGridView method with the params switched, so that DGV2 gets bound with DGV1's params? Also: Is gvPermissions_CellClick used to edit the cells? – TaW Dec 25 '14 at 13:04

0 Answers0