Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Difference between ListBox and CheckedListBox DataBinding

0.00/5 (No votes)
15 Aug 2011 1  
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.:


VB
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!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here