Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge 2 columns from datatable in datatextfield from dropdownlist

Tags:

c#

.net

var id = Session["staff_id"].ToString()

//I have datatable with 5 columns

DataTable dt = function_return_Datatable(id);

dropdownlist1.DataSource = dt;

/*in DataTextField I want to merge two columns of DataTable, dt.columns [1] is First Name and dt.columns [2] is LastName*/

//I tried this way to merge them, but no results
dropdownlist1.DataTextField = dt.Columns[1].ToString()+" "+dt.Columns[2].ToString();

dropdownlist1.DataValueField = dt.Columns[0].ToString();
dropdownlist1.DataBind();

Any ideas for how to merge these two columns?

like image 314
Alex Avatar asked Feb 14 '12 10:02

Alex


1 Answers

You'll need a full name column in your data table as DataTextField can refer to only one single field:

DataTable dt = function_return_Datatable(id);
dt.Columns.Add("FullName", typeof(string), "FirstName + ' ' + LastName");

dropdownlist1.DataSource = dt;
dropdownlist1.DataTextField = "FullName";
dropdownlist1.DataValueField = "ID";
dropdownlist1.DataBind();

Should do it

(you could also add this column in your SQL query)

like image 130
vc 74 Avatar answered Oct 21 '22 05:10

vc 74