DataGridView复选框未更新

时间:2015-04-08 10:23:39

标签: c# winforms checkbox datagridview

任何人都可以告诉我为什么我的复选框没有被检查? 我正在使用Windows.Forms并且Grid设置为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();

调试时我已经看到复选框的Value参数被赋予了正确的值,但由于某种原因,从不检查该框。

1 个答案:

答案 0 :(得分:0)

解决方案是,而不是像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);
        }
    }