Merge 2 columns from datatable to datatextfield from drop down list

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 on how to combine these two columns?

+6
source share
1 answer

You will need a column with a full name in your data table, because a DataTextField can only refer to one 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(); 

Gotta do it

(you can also add this column to your SQL query)

+24
source

Source: https://habr.com/ru/post/908414/


All Articles