【问题标题】:Bind a List to Data Grid View which contains Check Box Column将列表绑定到包含复选框列的数据网格视图
【发布时间】:2018-03-04 16:03:07
【问题描述】:

我很难将对象列表绑定到具有复选框列的数据网格视图。

即使我识别出 TrueValue 和 FalseValue,这些值也不会显示。

这就是我所做的

public class Tree{
  public bool Added{get;set;}
  public string TaskName{get;set;}
  }

这就是设计师的样子

    private System.Windows.Forms.DataGridViewCheckBoxColumn ClmnCheckBox;
    private System.Windows.Forms.DataGridViewTextBoxColumn ClmnName;
    this.DgvTrees.Location = new System.Drawing.Point(0, 26);
    this.DgvTrees.MultiSelect = false;
    this.DgvTrees.Name = "DgvNoneTasks";
    this.DgvTrees.RowHeadersVisible = false;
    this.DgvTrees.SelectionMode =System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        this.DgvTrees.Size = new System.Drawing.Size(505, 145);
        this.DgvTrees.TabIndex = 27;
        this.DgvTrees.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.DgvTrees_CellContentClick);
        this.DgvTrees.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.DgvTrees_CellValueChanged);



       this.ClmnCheckBox.DataPropertyName = "Added";
        this.ClmnCheckBox.FalseValue = "false";
        this.ClmnCheckBox.HeaderText = "Add/Remove";
        this.ClmnCheckBox.Name = "ClmnCheckBox";
        this.ClmnCheckBox.TrueValue = "true";
        // 
        // ClmnName
        // 
        this.ClmnName.DataPropertyName = "TaskName";
        this.ClmnName.HeaderText = "Task Name";
        this.ClmnName.Name = "ClmnName";
        this.ClmnName.ReadOnly = true;

这就是我将列表绑定到网格的方式

 var tree= new Tree(){
 Added=true,
 TaskName="task1"
 };
 ListOfTrees.Add(tree);
 DgvTrees.DataSource=ListOfTrees;

现在超出了这一点,我得到了记录,但未选中复选框。如果我手动检查它,它将被检查,但是一旦我刷新网格或对其进行排序,检查就消失了。这可能是什么原因造成的?

【问题讨论】:

  • 数据绑定仅适用于公共属性 -> 将类属性设为公共
  • 它们是公开的我只是没有编辑问题,一旦我应用过滤器或对列进行排序,值就会重置为未选中。

标签: c# forms winforms checkbox datagridview


【解决方案1】:

我通过监听事件 RowsAdded 来修复它,每次通过排序或过滤添加天气时,我将复选框设置为对象布尔值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-11
    • 2018-01-03
    • 2014-01-24
    • 2017-04-14
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多