all groups > dotnet windows forms > may 2005 >
You're in the

dotnet windows forms

group:

binding array to listbox


binding array to listbox bean
5/7/2005 7:54:53 AM
dotnet windows forms:
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;
Re: binding array to listbox Bud James via DotNetMonster.com
5/8/2005 12:00:00 AM
the simplest solution is to override the tostring() method of the object
you're binding to.

--
RE: binding array to listbox Luis Fajardo
5/13/2005 6:42:02 AM
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!



[quoted text, click to view]
AddThis Social Bookmark Button