首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataGridView复选框未更新

DataGridView复选框未更新
EN

Stack Overflow用户
提问于 2015-04-08 10:23:39
回答 1查看 1.3K关注 0票数 0

有人能告诉我为什么我的复选框不被检查吗?我使用的是Windows.Forms,网格设置为ReadOnly=False,下面是我使用的代码:

代码语言:javascript
复制
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);
        }
    }

在视图创建之后,我调用:

代码语言:javascript
复制
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.               

            }

当值被更改时,事件处理程序然后调用

代码语言:javascript
复制
        RouteRampView.EndEdit();
        RouteRampView.Refresh();

调试时,我看到复选框的Value参数被指定为正确的值,但出于某种原因,从未选中该复选框。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-22 12:09:45

解决方案是,与其将fillValues()称为其工作方式,不如:

代码语言:javascript
复制
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);
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29512031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档