65.9K
CodeProject is changing. Read more.
Home

Difference between ListBox and CheckedListBox DataBinding

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3 votes)

Aug 15, 2011

CPOL
viewsIcon

30361

The difference between ListBox and CheckedListBox DataBinding.

Thought I would post this tip as it took me a little while to find the answer.

I had sucessfully bound my custom collection class to a ListBox control on a WinForm. However, when I attempted to bind to a CheckedListBox, it was populated with what appeared to be the ToString method of the collection's items, instead of the property specified for the DisplayMember.

I had read previously (http://msdn.microsoft.com/en-us/magazine/cc163630.aspx#S4[^]) that it is more efficient to set the DisplayMember and ValueMember properties before settings the DataSource property. Doing so prevents control refreshes.

However, a CheckedListBox does not display correct values unless the DataSource is set first.

E.g.:

With ListBox1
  .DataSource = Nothing
  .DisplayMember = "Source"
  .ValueMember = "Destination"
  .DataSource = MyCollection
End With

With CheckedListBox1
  .DataSource = Nothing
  .DataSource = MyCollection
  .DisplayMember = "Source"
  .ValueMember = "Destination"
End With

Hope this saves someone some time!