繁体   English   中英

将列表绑定到包含复选框列的数据网格视图

[英]Bind a List to Data Grid View which contains Check Box Column

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

即使我标识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;

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

我通过听事件“ Row Attached”来解决问题,每次通过排序或过滤将天气添加到行中时,都将复选框设置为对象布尔值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM