简体   繁体   中英

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

I am having a hard time binding a list of objects to a data grid view which has check box columns.

The values do not show even when I identify the TrueValue, and the FalseValue.

Here is what I did

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

in the form this is what the designer looks like

    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;

and this is how I bind the list to the grid

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

Now beyond that point, I get the record, but the checkbox is not checked. If I manually check it, it will get checked, but as soon as I refresh the grid, or sort it the check is gone. What could be causing this ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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