【问题标题】:Grid with checkbox column not editable带有复选框列的网格不可编辑
【发布时间】:2014-01-13 18:16:19
【问题描述】:

我是 C# 新手。
我有一个 GridView + BindingSource。
我用这样的项目列表填充我的绑定源:

        public class ListItem
    {
        public DbObject EntityObject { get; set; }
        public bool Used
        {
            get
            {
                return EntityObject != null;
            }
        }

        public int Id
        {
            get
            {
                return EntityObject == null ? 0 : EntityObject.Id;
            }
        }

        public string Name{
            get
            {
               return EntityObject == null ? "<no name>" : EntityObject.ToString(); 
            } 
        }
    }

数据在网格中显示正常。但我无法选中或取消选中 Grid 中的复选框。当我没有填写字段Used

        public bool Used
        {
           get; set;
        }

网格再次变得可编辑。 这里有什么问题吗?

更新

现在我的网格是可编辑的,但不能正常工作。
我有 4 个 raws,检查了 2 个 raws:

1 unchecked
2 unchecked
3 checked
4 checked

现在我取消选中原始 3:

1 unchecked
2 unchecked
3 unchecked
4 checked

没关系。现在取消选中 raw 4:

1 unchecked
2 unchecked
3 checked
4 unchecked

现在检查原始 2:

1 unchecked
2 checked
3 checked
4 checked

为什么 Grid 工作起来如此奇怪?

【问题讨论】:

    标签: c# winforms gridview bindingsource


    【解决方案1】:

    目前你有只读属性,如果你添加set也可以编辑

    private bool temp;
    
    public bool Used
    {
        get { return temp; }
        set { temp= value; }
    }
    

    【讨论】:

    • 网格变得可编辑。但这里有些奇怪。我在网格中有 4 个原始数据。检查了2个生的。现在我想取消选中所有原始但我不能。我更新问题请看。
    • 你做了什么改变?你改变了Used 属性吗?
    • 我被添加:set{ var temp = value; }Used 属性;
    • 将我的代码与private bool temp;一起使用并同时获取和设置,不要在设置器中使用var
    • 您的代码有效。但是当我输入自己的“get{}”时,我得到了相同的结果。但是感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 2013-04-17
    • 2016-08-10
    • 1970-01-01
    • 2023-03-18
    • 2013-03-27
    相关资源
    最近更新 更多