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

Discussion in 'ASP .Net' started by Mark Rae, Jan 5, 2007.

  1. Mark Rae

    Mark Rae Guest

    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
     
    Mark Rae, Jan 5, 2007
    #1
    1. Advertising

  2. Mark,

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

    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.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Mark Rae" <> wrote in message
    news:...
    > 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
    >
     
    Eliyahu Goldin, Jan 7, 2007
    #2
    1. Advertising

  3. Mark Rae

    Mark Rae Guest

    "Eliyahu Goldin" <> wrote in
    message news:...

    > 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...
     
    Mark Rae, Jan 7, 2007
    #3
  4. 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.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin


    "Mark Rae" <> wrote in message
    news:...
    > "Eliyahu Goldin" <> wrote in
    > message news:...
    >
    > > 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...
    >
    >
     
    Eliyahu Goldin, Jan 7, 2007
    #4
  5. RE: Using more than one database field for a DropDownList's DataTextFi

    SELECT emp_id, lname + ', ' + fname As empName FROM employees

    Hope this helps...
     
    =?Utf-8?B?S0pH?=, Jan 12, 2007
    #5
  6. Mark Rae

    Mark Rae Guest

    Re: Using more than one database field for a DropDownList's DataTextFi

    "KJG" <> wrote in message
    news:...

    > SELECT emp_id, lname + ', ' + fname As empName FROM employees


    Did you actually read the original post...?

    > Hope this helps...


    How...?
     
    Mark Rae, Jan 12, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    4
    Views:
    535
    Alan Silver
    Jul 3, 2005
  2. mimi
    Replies:
    2
    Views:
    3,843
  3. l7alabeh
    Replies:
    4
    Views:
    2,646
    l7alabeh
    Sep 20, 2006
  4. Steven D'Aprano
    Replies:
    0
    Views:
    143
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    116
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page