• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1085
  • Last Modified:

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!

0
bdrvm
Asked:
bdrvm
  • 2
  • 2
1 Solution
 
tusharashahCommented:
One of the easy way I know, is to modify SQL Query like following

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


DropDownList1.DataTextField = "Name";

-tushar
0
 
bdrvmAuthor Commented:
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"];

0
 
tusharashahCommented:
Here's your modified code:


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

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

-tushar
0
 
bdrvmAuthor Commented:
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!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now