2014-02-05 41 views
0

在我的DataGridView上,我已將AllowUserToDeleteRows設置爲True。當DataSource爲List時,DataGridView刪除行列表<myClass>

當我把我的DataGridViewDataSourceIQueryable

var dc = new Data.CustomersDataContext(); 
var q = dc.Customers; 
dataGridView1.DataSource = q; 

我可以從它刪除行,但是當我將其設置爲List<T>

var dc = new Data.CustomersDataContext(); 
var l = dc.Customers.ToList(); 
dataGridView1.DataSource = l; 

我也沒有更多的刪除行(當我按下刪除按鈕時沒有任何反應)

如何將我的DataSource保留爲List<T>也能夠刪除行?

+0

是否客戶有結構或類instaces列表? – Junaith

+0

@Junaith我不知道。這是Linq-to-Sql生成的。 –

+0

@Junaith我檢查了我的dbml Designer,它是Customer的列表,其中Customer是從INotifyPropertyChanging派生的類,INotifyPropertyChanged –

回答

1

發生這種情況是因爲DataGridView只允許在與IBindingList實現(請參見下面的註釋)綁定時刪除行,並且IBindingList.AllowRemove返回true。

你可以用你的名單分成BindingList<T>,它允許在默認情況下刪除項目:

dataGridView1.DataSource = new BindingList<Customer>(dc.Customers.ToList()); 

注意。Data.CustomersDataContext.Customers implements IListSource,它返回與AllowRemove == true,這就是爲什麼你的第一個案件按預期工作。 DGV知道IListSource並使用IListSource.GetList()結果作爲數據源。

相關問題