binding array to listbox

  • Thread starter Thread starter bean
  • Start date Start date
B

bean

Hi,
I'm having problems binding an array of objects to a listbox. The
objects are from a webservices class running on our web server.
The array is coming from the website fine, and the data is binding ok
to the listbox, but when I run the program, the listbox just comes up
with the name of the object rather than the DisplayMember property I
have
set. All other operations getting data out of the array are working as
expected.

Does anyone have a clue what I'm doing wrong?

TIA,
Ian

Code:
shop.WSCategories wscategories = new shop.WSCategories();
shop.Category[] cats = wscategories.GetCategories();
lbCats.DisplayMember = "Name";
lbCats.ValueMember = "Id";
lbCats.DataSource = cats;
 
the simplest solution is to override the tostring() method of the object
you're binding to.
 
I'm sorry I don't have the time to test what I'm saying here, but give it a
try yourself and see if it works.

I believe that when you assing the DataSource property, that DisplayName
gets reset. So, change the order of your code to:

Code:
shop.WSCategories wscategories = new shop.WSCategories();
shop.Category[] cats = wscategories.GetCategories();
lbCats.DataSource = cats;
lbCats.DisplayMember = "Name";
lbCats.ValueMember = "Id";


First the DataSource and then the DisplayMember. Also make sure that your
"cats" object exposes a public property named "Name". But I'm almost
positive that your problem is related to the sequence of the lines. I might
be wrong, so test it yourself and let us know how it goes!.


Good luck!
 
Back
Top