PC Review


Reply
Thread Tools Rate Thread

Binding an ArrayList of custom objects to a dropdownlist

 
 
=?Utf-8?B?RGF2aWQgR3V0aHU=?=
Guest
Posts: n/a
 
      31st Jul 2004
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!

--
David Guthu
 
Reply With Quote
 
 
 
 
=?Utf-8?B?QmhhcmF0IEJpeWFuaQ==?=
Guest
Posts: n/a
 
      2nd Aug 2004
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).

--
Bharat Biyani ((E-Mail Removed))
http://www.orcim.com



"David Guthu" wrote:

> 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!
>
> --
> David Guthu

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDownList Bound to ArrayList of Custom Objects phlian@comcast.net Microsoft ASP .NET 3 30th May 2005 03:16 PM
Binding to deserialized objects in an arraylist !? Alex Nitulescu Microsoft ASP .NET 5 21st Feb 2005 12:21 AM
Binding ArrayList full of custom objects to DataGrid berg Microsoft C# .NET 0 25th Jun 2004 06:10 PM
Binding a Repeater control to an ArrayList of objects Abhishek Srivastava Microsoft ASP .NET 2 5th Feb 2004 02:44 PM
Binding a DataList to an ArrayList of objects Stefan Landgraf Microsoft ASP .NET 3 24th Nov 2003 10:41 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:51 AM.