Using more than one database field for a DropDownList's DataTextField property

M

Mark Rae

Hi,

Here's one just for my own interest - I know how to add ListItems
programatically, and how to concatenate fields in SQL queries...

But, is there any way to specify anything other than a single database field
in the DataTextField of a DropDownList?

E.g. let's say I have the following SQL Server table called MyUsers:

UserID FirstName LastName
----------------------------------
1 Juan Libre
2 Peter Bromberg
3 John Timney
4 Mark Rae

And I populate my DropDownList like this:

string strSQL = "SELECT UserID, FirstName, LastName FROM MyUsers";
using (SqlDataReader MyReader = GetSqlDataReader(strSQL)
{
cmbUsers.DataSource = MyReader;
cmbUsers.DataValueField = "UserID";
cmbUsers.DataTextField = "LastName";
cmbUsers.DataBind();
}

But is there any way (other than adding the ListItems manually or changing
the SQL) to persuade the DataTextField to concatenate the LastName and
FirstName fields so that it displays:

Libre, Juan
Bromberg, Peter
Timney, John
Rae, Mark

As I mentioned, just for my own interest...

Any assistance gratefully received.

Mark
 
M

Mark Rae

Does it have to be an <asp:dropdownlist>?

Please read the OP again...
You can achieve the desired effect with a html <select> if you put the
<option>s in a repeater and databind them as you wish.

Or by adding the ListItems individually...

Or by changing the SQL...

Please read the OP again...
 
E

Eliyahu Goldin

Mark,

You, as a frequent and valuable contributor to this newsgroup, know very
well that posters often ask very specific questions how to achieve the
desired effect in a particular way whereas the correct answer as to take
another way.

I understood your question that you don't want to add the items
programmatically, neither you want to change the sql, but you do want to
change just the markup.

Had we always answered in a strictly formal way, this newsgroup would've not
been as useful as it is.
 

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top