DataGridView Tag value for rows is null

941 Views Asked by At

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();
        }
    }
0

There are 0 best solutions below