2009-10-14 24 views
12

我已經DataGridView綁定到List<myClass>DataSource。 但是,當我將「AllowUserToAddRows」屬性設置爲「True」時,沒有任何內容出現。不能允許用戶添加行到DataGridView與列表<>數據源

我試圖將DataSource更改爲BindingList<myClass>,它進展順利。

不知我是否應該用BindingList<>替換我的List<>或者有更好的解決方案。

回答

21

myClass有一個公共無參數構造函數嗎?如果沒有,您可以從BindingList<T>派生並覆蓋AddNewCore以調用您的自定義構造函數。

(編輯),或者 - 只是包裝你的列表中BindingSource,它可能工作:

using System; 
using System.Windows.Forms; 
using System.Collections.Generic; 
public class Person { 
    public string Name { get; set; } 

    [STAThread] 
    static void Main() { 
     var people = new List<Person> { new Person { Name = "Fred" } }; 
     BindingSource bs = new BindingSource(); 
     bs.DataSource = people; 

     Application.Run(new Form { Controls = { new DataGridView { 
      Dock = DockStyle.Fill, DataSource = bs } } }); 
    } 
} 
+0

這工作:)謝謝ü非常。 – 2009-10-14 12:29:26

相關問題