2012-06-18 68 views
5

我不明白爲什麼我的DataGridView不斷爲空(沒有行,沒有自動生成的列):的BindingSource/DataGridView的互動

BindingList<MyObject> bList = new BindingList<MyObject>(); 
fileStream.Position = 0; 
MyObject.Deserialize(fileStream).ForEach(
    j => bList.Add(j)); 

this.bindingSource1.SuspendBinding(); 

this.dataGridView1.Columns.Clear(); 
this.dataGridView1.AutoGenerateColumns = true; 
this.dataGridView1.Enabled = false; 
this.dataGridView1.Invalidate(); 
this.bindingSource1.DataSource = bList; 
this.dataGridView1.DataSource = bindingSource1; 
this.bindingSource1.ResumeBinding(); 
this.dataGridView1.Enabled = true; 
this.dataGridView1.Refresh(); 

其中myObject的是,如果我可明確定義爲

public class MyObject 
{ 
    public DateTime CreationDate; 
    public string CreationId; 

    public static List<MyObject> Deserialize(Stream s) 
    { 
     XDocument xml = XDocument.Load(s); 

     var ps = from p in xml 
         .Descendants("p") 
         .Descendants("object") 
        select 
         new MyObject 
         { 
          CreationId = p.Attribute("creationid").Value 
         }; 

     return ps.ToList(); 
    } 

} 

另外設置列如下,行添加到網格,但它們都是空的

DataGridViewTextBoxColumn dc = new DataGridViewTextBoxColumn(); 
      dc.DataPropertyName = "CreationDate"; 
      dc.HeaderText = "CreationDate"; 
      dc.Name = "CreationDate"; 
      dc.Visible = true; 
      dc.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; 
      this.dataGridView1.Columns.Add(dc); 

      dc = new DataGridViewTextBoxColumn(); 
      dc.DataPropertyName = "CreationId"; 
      dc.HeaderText = "CreationId"; 
      dc.Name = "CreationId"; 
      dc.Visible = true; 
      dc.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; 
      this.dataGridView1.Columns.Add(dc); 
+0

只是快速的觀察 - 1.嘗試重命名'creationid'就像'CreationId'完全一樣,在對象中缺少網格視圖的'DataProperty'和'CreationDate'。 –

+0

僅當'Autogeneratecolumn設置爲false' - >場景 –

+0

False時,以上註釋纔有效。 creationid是用這種方式寫在xml中的。您的解決方案不起作用。 – Mauro

回答

4

在一個Winforms項目,試試這個代碼(一種你貼什麼& it works !模擬器)&然後映射/調試到您現有的代碼,以縮小問題: -

DataGridView dgv = new DataGridView(); 
    //Note: AutogenerateColumns is true by default 
    BindingSource bs = new BindingSource(); 
    BindingList<Customer> bList = new BindingList<Customer>(); 

    // Fill bList with Customers 
    bList.Add(new Customer(){Name="John"}); 

    bs.DataSource = bList; 
    dgv.DataSource = bs; 

    this.Controls.Add(dgv); 

爲MyObject相當於: -

public class Customer 
    { 
     public string Name { get; set; } 
    } 

所以,to Debug your code通過映射到上述一個,您可以刪除這些位 -

this.bindingSource1.SuspendBinding(); 
this.dataGridView1.Columns.Clear(); 
this.dataGridView1.AutoGenerateColumns = true; 
this.dataGridView1.Enabled = false; 
this.dataGridView1.Invalidate(); 
this.bindingSource1.ResumeBinding(); 
this.dataGridView1.Enabled = true; 
this.dataGridView1.Refresh(); 

& just have this在第一審判 -

this.bindingSource1.DataSource = bList; 
this.dataGridView1.DataSource = bindingSource1; 
+0

不工作。對不起 – Mauro

+0

你應該更好地指出我錯過了{get;設置;}但+1快速回答 – Mauro