DropDownList having a DataTextField value of two columns from one DataTable (C#)

Posted on 2004-11-08
Last Modified: 2011-10-03
Hi everyone,

Is there a way of giving 2 columns of one DataTable in the DataTextField of a DropDownList?
Code snippet always appreciated!

Question by:bdrvm
    LVL 18

    Expert Comment

    One of the easy way I know, is to modify SQL Query like following

    SELECT Name = (FirstName + ' ' + LastName) From Addresses;

    DropDownList1.DataTextField = "Name";


    Author Comment

    Here is my code:

    DataSet myDataSet = new DataSet();
    OleDbDataAdapter myOleDbDataAdapter = new OleDbDataAdapter("Select * From T_Employee", myConnection);

    DDLemployee.DataSource = myDataSet.Tables["Employee"];
    DDLemployee.DataTextField = " ? "

      // What I want is something like
      //myDataSet.Tables["Employee"].Columns["FirstName"] + " " + myDataSet.Tables["Employee"].Columns["LastName"];
     // but how?

    DDLemployee.DataValueField = myDataSet.Tables["Employee"].Columns["id"];

    LVL 18

    Accepted Solution

    Here's your modified code:

    OleDbDataAdapter myOleDbDataAdapter = new OleDbDataAdapter("Select  Name =(FirstName + ' ' + LastName),* From T_Employee", myConnection);

    DDLemployee.DataSource = myDataSet.Tables["T_Employee"];
    DDLemployee.DataTextField = "Name";
    DDLemployee.DataValueField = myDataSet.Tables["T_Employee"].Columns["id"];


    Author Comment

    by:bdrvm found it !.

    Here is my final code

    OleDbDataAdapter myOleDbDataAdapter = new OleDbDataAdapter("Select  *, FirstName + '  ' + LastName as empName  From T_Employee", myConnection);

    DDLemployee.DataSource = myDataSet.Tables["T_Employee"];
    DDLemployee.DataTextField = "empName";

    it work!

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now