Ayush
Ayush

Reputation: 42450

How can i add a data object of a class array to a drop down list?

I have an array for type Person

Person[] Traders = GetTraders();

Person class contains data objects such as first name, last name etc.

I want to add all first names to a dropdownlist. How can i do that? I tried doing it like this, but it won't get the first names:

ddl_traders.DataSource = traders;

EDIT

Person has the following string fields: FirstName, LastName, login.

I want the dropdownlist to display FirstName, but the value has to be the logins. I'm fairly certain that is possible, although I have no idea how it can be done. Any suggestions?

Upvotes: 0

Views: 455

Answers (2)

Andrew
Andrew

Reputation: 14447

One way:

ddl_traders.DataSource = GetTraders().OfType<Person>().Select<Person, string>(p => p.FirstName).ToList<string>();

This depends on Person having a string field called FirstName.

Upvotes: 1

pattertj
pattertj

Reputation: 421

This may not be the best way, but you can just specify the field to be diplayed like:

    ddl_traders.DateSource = traders;
    ddl_traders.DataTextField = "FirstName";
    ddl_traders.DateValueField= "login";
    ddl_traders.DataBind();

This allows you to bind the full Person Object, but only display the name, and keep the login as the value.

Upvotes: 1

Related Questions