Link to home
Start Free TrialLog in
Avatar of bdrvm
bdrvm

asked on

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

Hi everyone,

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

Avatar of tusharashah
tusharashah

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

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


DropDownList1.DataTextField = "Name";

-tushar
Avatar of bdrvm

ASKER

Here is my code:

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

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"];

ASKER CERTIFIED SOLUTION
Avatar of tusharashah
tusharashah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bdrvm

ASKER

Thanks...you found it !.

Here is my final code

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

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

it work!