Click here to Skip to main content
Click here to Skip to main content

Difference between ListBox and CheckedListBox DataBinding

, 15 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Paul_Williams
Engineer Computer Station (Cymru) Ltd.
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 5 Wow, that is a subtle problem. Thank... PinsubeditorWalt Fair, Jr.15-Aug-11 14:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 15 Aug 2011
Article Copyright 2011 by Paul_Williams
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid