DataGridView 复选框未更新

DataGridView checkbox not updating

谁能告诉我为什么我的复选框没有被选中? 我正在使用 Windows.Forms 并且网格设置为 ReadOnly=False 这是我正在使用的代码:

private void populateView() 
    {
        try
        {
            DataTable theTable = [query execution]
            DataView dv = theTable.DefaultView;
            dataGrid1.DataSource = dv;
            DataGridViewCheckBoxColumn[] checkBoxes = new DataGridViewCheckBoxColumn[20];
            int pos = 0;

            foreach (DataGridViewColumn column in RouteRampView.Columns)
            {
                if (column.Name.Equals("A"))
                {
                    column.Visible = false;
                }
                if (column.Name.Equals("B"))
                {
                    column.Visible = true;
                    column.ReadOnly = true;
                }
                if (column.Name.Equals("C"))
                {
                    column.Visible = true;
                    column.ReadOnly = true;
                }
                if ( column.Name.Contains("D") )
                {
                    var doWork = new DataGridViewCheckBoxColumn();
                    doWork.Name = column.Name +"BOX";
                    doWork.HeaderText = column.Name;
                    doWork.FalseValue = "0";
                    doWork.TrueValue = "1";
                    column.Visible = true;
                    checkBoxes[pos] = doWork;
                    pos++;
                }
            }
            foreach (DataGridViewCheckBoxColumn column in checkBoxes)
            {
                if (column != null)
                {
                    dataGrid1.Columns.Add(column);
                }

            }

        }
        catch (Exception e)
        {
            log.Error("Error occured when populating grid", e);
        }
    }

创建视图后我调用:

private void fillValues()
        {
            foreach (DataGridViewRow row in dataGrid1.Rows)
            {
                DataGridViewCheckBoxCell col1 = (DataGridViewCheckBoxCell)row.Cells["D1BOX"];
                col1.Selected = ((Decimal)row.Cells["D1"].Value > 0);
                col1.ThreeState = ((Decimal)row.Cells["D1"].Value > 0);
                col1.Value = ((Decimal)row.Cells["D1"].Value > 0 ? col1.TrueValue : col1.FalseValue );


                DataGridViewCheckBoxCell col2 = (DataGridViewCheckBoxCell)row.Cells["D2BOX"];
                col2.Selected = ((Decimal)row.Cells["D2"].Value > 0);
                col2.ThreeState = ((Decimal)row.Cells["D2"].Value > 0);
                col2.Value = ((Decimal)row.Cells["D2"].Value > 0 ? col1.TrueValue : col1.FalseValue);

                DataGridViewCheckBoxCell col3 = (DataGridViewCheckBoxCell)row.Cells["D3BOX"];
                col3.Selected = ((Decimal)row.Cells["D3"].Value > 0);
                col3.ThreeState = ((Decimal)row.Cells["D3"].Value > 0);
                col3.Value = ((Decimal)row.Cells["D3"].Value > 0 ? col1.TrueValue : col1.FalseValue);

                DataGridViewCheckBoxCell col4 = (DataGridViewCheckBoxCell)row.Cells["D4BOX"];
                col4.Selected = ((Decimal)row.Cells["D4"].Value > 0);
                col4.ThreeState = ((Decimal)row.Cells["D4"].Value > 0);
                col4.Value = ((Decimal)row.Cells["D4"].Value > 0 ? col1.TrueValue : col1.FalseValue);

                DataGridViewCheckBoxCell col5 = (DataGridViewCheckBoxCell)row.Cells["D5BOX"];
                col5.Selected = ((Decimal)row.Cells["D5"].Value > 0);
                col5.ThreeState = ((Decimal)row.Cells["D5"].Value > 0);
                col5.Value = ((Decimal)row.Cells["D5"].Value > 0 ? col1.TrueValue : col1.               

            }

当值更改时,事件处理程序会调用

        RouteRampView.EndEdit();
        RouteRampView.Refresh();

在调试时,我发现复选框的值参数被赋予了正确的值,但由于某种原因,该框从未被选中。

解决方案不是调用 fillValues(),而是像这样调用:

private void populateView() 
    {
        theView.Refresh();

        try
        {
            DataTable theTable = [query execution];
            DataTable clonedDt = theTable.Clone();
            for (int i = 2; i < clonedDt.Columns.Count -1 ; i++ )
            {
                clonedDt.Columns[i].DataType = typeof(Boolean);
            }

            foreach (DataRow row in theTable.Rows)
            {
                clonedDt.ImportRow(row);
            }

            DataView dv = clonedDt.DefaultView;
            theView.DataSource = dv;
            DataGridViewCheckBoxColumn[] checkBoxes = new DataGridViewCheckBoxColumn[20];

            foreach (DataGridViewColumn column in theView.Columns)
            {
                if (column.Name.Equals("A"))
                {
                    column.Visible = false;
                }
                if (column.Name.Equals("B"))
                {
                    column.Visible = true;
                    column.ReadOnly = true;
                }
                if (column.Name.Equals("C"))
                {
                    column.Visible = true;
                    column.ReadOnly = true;
                }
                if ( column.Name.Contains("D") )
                {
                    column.Visible = true;
                    column.ReadOnly = false;
                }   
            }
        }
        catch (Exception e)
        {
            log.Error("Error occured when populating grid", e);
        }
    }