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 开发者_如何学JAVAbe the logins. I'm fairly certain that is possible, although I have no idea how it can be done. Any suggestions?
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.
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.
精彩评论