Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2004

Tip: Looking for answers? Try searching our database.

Binding an ArrayList of custom objects to a dropdownlist

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Guthu - 31 Jul 2004 18:57 GMT
How do I bind an arraylist of custom objects to a dropdownlist and set the datavaluefield and datatextfields?

Let's say I have an array of custom 'Person' objects.

public class Person
{
public string FirstName;
public string LastName;
....
}

I want to bind an array of Person objects and use the DataTextField and DataValue field of the DropDown to bind:
ie:

...
ddList.DataSource = PersonObjectArray()
ddList.DataTextField = "FirstName";
ddList.DataValueField = "LastName"; ddList.DataBind();

Can this be done?
I've tried creating a PersonCollection class which inherits ArrayList but I'm still unsure how to access the properties of the objects within the arraylist to set the above values. Please help!

Signature

David Guthu

Bharat Biyani - 02 Aug 2004 06:13 GMT
Hi David,

Expose the member variables of the object that you would like to bind to the combobox as properties.

Then set :
combobox.datasource= <array name variable>
combobox.datatextfield= <property to display>
combobox.datavaluefield=<property to be used as value>

eg:
ArrayList userlist=new ArrayList();
userlist.Add(new User("John",1));
userlist.Add(new User("Johnny",2))
userlist.Add(new User("Jenny",3))
ddl1.DataSource=userlist;
ddl1.DataTextField="UserName";
ddl1.DataValueField="Id";
ddl1.DataBind();

In the above eg. User is an object which exposes the UserName and Id properties which are bound to the dropdownlist (ddl1).

Signature

Bharat Biyani (bsb@orcim.com)
http://www.orcim.com

> How do I bind an arraylist of custom objects to a dropdownlist and set the datavaluefield and datatextfields?
>
[quoted text clipped - 17 lines]
> Can this be done?
> I've tried creating a PersonCollection class which inherits ArrayList but I'm still unsure how to access the properties of the objects within the arraylist to set the above values. Please help!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.