DataGridView行的Tag值为null

时间:2014-12-22 23:26:21

标签: c# .net winforms datagridview datagridviewcheckboxcell

我在表单上有2个DataGridView控件。两者都具有相同数量和类型的列。 1个TextBox列和2个CheckBoxColumns。 问题是第一个DataGridView工作正常但另一个不工作。两者都有相同的绑定方法和数据源。第二个DataGridView在同一表单上的问题是..

  • 未设置复选框值

  • currentrow。当我尝试检索值时,标签值为空

下面是我用来绑定DataGridViews和设置复选框值的代码

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 个答案:

没有答案