簡體   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