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

Difference between ListBox and CheckedListBox DataBinding

By , 15 Aug 2011
 

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)

About the Author

Paul @ The Computer Station
Engineer The Computer Station (Cymru) Ltd.
United Kingdom United Kingdom
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 5 Wow, that is a subtle problem. Thank...subeditorWalt Fair, Jr.15 Aug '11 - 13:24 
Reason for my vote of 5
Wow, that is a subtle problem. Thanks.

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 15 Aug 2011
Article Copyright 2011 by Paul @ The Computer Station
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid