开发者

Binding a .NET WinForms ListBox to a ConnectionStringSettingsCollection

I am trying to bind the ConnectionStringsSettingsCollection from my app.config file to a ListBox in my WinForms app. For some reason, I can't seem to do it by the following method:

lstMyListBox.DataSource = GetConnectionStrings()
lstMyListBox.DisplayMember = "Name"

I am able to do it by iterating over the collection and adding each Connection开发者_如何学运维StringSetting to lstMyListBox.Items:

For Each settings As ConnectionStringSettings In GetConnectionStrings()
    lstMyListBox.Items.Add(settings)
Next
lstMyListBox.DisplayMember = "Name"

Which works as far as modifying the individual items that are already in the collection, but I would like it if I remove something from the ListBox that it is removed from the underlying collection. Is there something I can do different that will allow me to bind the ListBox directly to the ConnectionStringSettingsCollection?


ConnectionStringsSettingsCollection doesn't implement IList or IListSource, so it can't be used as a DataSource. Instead you can bind a list of ConnectionStringSettings:

lstMyListBox.DataSource = GetConnectionStrings().Cast(Of ConnectionStringSettings).ToList()
lstMyListBox.DisplayMember = "Name"

However the DisplayMember doesn't seem to be taken into account, at least not for all items... but it works fine if you use "ConnectionString" as the DisplayMember. Not sure why...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜