views:

26

answers:

2

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?

+1  A: 

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.

Andrew
Person does have a string field called FirstName. I'll give it a try.
xbonez
PS - I made an edit to the question. Would you know how to do that?
xbonez
Tried your solution. Getting this error: Error 1 'System.Array' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
xbonez
@xbonez My bad -- I just edited. Try now...
Andrew
@xbonez Regarding your edit, @pattertj basically has it right. You would specify the DataTextField and DataValueField as strings, and your DataSource could probably just get GetTraders(). Then, you would call DataBind(), just as you would with the method I showed before.
Andrew
+1  A: 

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.

pattertj
I'm attempting to try out your solution but am getting th dumb 'shadow copy' error. As soon as I have that worked out, I'll try your solution. Thanks though.
xbonez
Alright. Works great. Thanks!
xbonez