2012-06-18 23 views
5

Tôi không thể hiểu tại sao DataGridView của tôi vẫn tiếp tục là rỗng (không có hàng, không có cột autogenerated):BindingSource/DataGridView tương tác

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(); 

nơi MyObject được định nghĩa là

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(); 
    } 

} 

Bên cạnh đó nếu tôi rõ ràng mới đặt các cột như sau, các hàng được thêm vào lưới nhưng tất cả các hàng này đều trống rỗng

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

Chỉ cần nhanh chóng quan sát - 1. Cố gắng Đổi tên 'creationid' là 'CreationId' giống như' DataProperty' và 'CreationDate' của khung nhìn lưới bị thiếu trong Object. –

+0

Nhận xét trên chỉ hợp lệ nếu 'Autogeneratecolumn được đặt thành false' -> scenario –

+0

Sai. creationid được viết theo cách đó trong xml. Giải pháp của bạn sẽ không hoạt động. – Mauro

Trả lời

4

Trong một Winforms dự án, hãy thử mã này (loại mô phỏng những gì bạn đã gửi & it works !) & sau đó bản đồ/debug mã hiện tại của bạn để thu hẹp vấn đề: -

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 tương đương : -

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

Vì vậy, to Debug your codebởi bản đồ để trên một, bạn có thể loại bỏ những bit -

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 trong thử nghiệm 1 -

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

Không hoạt động. Xin lỗi – Mauro

+0

Bạn nên đã chỉ ra rằng tôi đã bỏ lỡ {get; set;} nhưng +1 cho câu trả lời nhanh – Mauro