【问题标题】:DataGridView checkbox not updatingDataGridView 复选框未更新
【发布时间】:2015-04-08 10:23:39
【问题描述】:

谁能告诉我为什么我的复选框没有被选中? 我正在使用 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 参数被分配了正确的值,但由于某种原因,该框从未被选中。

【问题讨论】:

  • 你设置了 AutoPostBack='true' 吗?
  • 我没有设置或取消设置 AutoPostBack 所以它应该有它的默认值
  • @Serv 这是WinForms,我很确定AutoPostBack 是这里的东西。
  • @Anthony 是的 - 隧道视觉。对不起。

标签: c# winforms checkbox datagridview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多