Groups | Blog | Home
all groups > dotnet windows forms databinding > june 2006 >

dotnet windows forms databinding : ComboBox binding, best approach?


Sam Carleton
6/27/2006 6:24:45 AM
Here is a simple question:

There is a list of objects in a collection that need to be displayed in
a ComboBox. The text that should be displayed is NOT the ToString(),
but the property LongDisplayName. What is the best way to display this
collection?

Should I be using a BindingSource or simply assigning the collection to
the combo box?

How do I go about getting the combo box to display the LongDisplayName
property rather then the ToString() return value?

THK!
Rimmer
6/29/2006 6:37:46 PM
Hi Sam,

I my experience with databinding to a combo box, it has always been
more reliable to convert the collection into a basic table. This may
not be the most elegant but for me has been the most reliable.

1. create a new table.
2. add 2 columns, id and name.
3. add a row for each collection item, in your case using "long display
name" as the name.
4. bind combo to new table, setting displaymember and valuemember.

this can be encapsulated in a common routine that can be reused for
many combos. again not that pretty but I feel it is the most reliable /
robust solution and one that I use for all my combo binding.

I will be watching this thread to see if there are any other
suggestions though!

Thanks
Anthoney
www.nowpromote.com.au

[quoted text, click to view]
Peter Monadjemi
7/12/2006 9:14:03 PM
Rimmer schrieb:
[quoted text, click to view]
I can't see any reason not to bind to a collection directly:

Bs.DataSource = Teams
With liTeams
.DisplayMember = "Name"
.ValueMember = "FlagPath"
.DataSource = Bs
End With

Teams is a List(Of Team) and Team is a simple class with a few properties.

But you should use the new BindingSource object which makes the binding
a lot easier:

Bs = New BindingSource()
Bs.DataSource = Team

Peter

PS: I might be complete wrong, though;)
AddThis Social Bookmark Button