2011-06-15 98 views
1

我試圖將DataGridView綁定到MembershipUserCollection,但網格拒絕顯示任何用戶,即使數據庫中有記錄。將WinForms DataGridView綁定到MembershipUserCollection

根據我在互聯網上的研究,我做了以下內容:

MembershipUserCollection x = Membership.GetAllUsers(); 

gvUsers.DataSource = x; 

感謝你的幫助。

+0

'x.Count!= 0'? – InBetween 2011-06-15 12:28:20

+0

是的,我檢查了x.count = 3 – AbZy 2011-06-15 12:29:16

回答

2

的問題是,您綁定到需要的對象來實現這些接口

  • IList
  • IListSource
  • IBindingList
  • IBindingView

之一而MembershipUserCollection只實現ICollection

一種解決方案是創建一個實現這些接口之一的包裝器/適配器。


這可能工作,不能測試atm。

var collection = Membership.GetAllUsers(); 
var bindingList = new BindingList<MemberShipUser>(); 

// If it's only meant for display 
bindingList.AllowNew = false; 
bindingList.AllowRemove = false; 
bindingList.AllowEdit = false; 

foreach (MemberShipUser member in collection) 
{ 
    bindingList.Add(member); 
} 
+0

有關如何創建此包裝器/適配器的任何提示? – AbZy 2011-06-15 12:38:36

+0

我更新了我的帖子。 – 2011-06-15 12:49:57

+0

很好地工作,謝謝。 – AbZy 2011-06-15 12:55:34

1

也許這很明顯,但我不得不從MemberShipUser到MembershipUser的小調整。

設置bindingList作爲我的數據網格的數據源,它工作的很棒!

相關問題