ASP.NET - how to add two column in dropdownlist

Asked By sunil pandey on 26-Jan-12 04:08 AM
hi all

i have firstname and lastname two different column in table i want to show as a cancatanation of both and bind it in ddl.
Sandeep Mittal replied to sunil pandey on 26-Jan-12 04:14 AM
SqlCommand cmd = new SqlCommand("select firstname+' '+lastname as fullname from tablename", con);
SqlDataReader dr = cmd.ExecuteReader();
DropDownList1.DataSource = dr;
DropDownList1.DataValueField = "fullname";
DropDownList1.DataTextField = "fullname";
DropDownList1.DataBind();
dipa ahuja replied to sunil pandey on 26-Jan-12 04:23 AM
void bindDropDonw()
{
  SqlDataAdapter da = new SqlDataAdapter("select empid,ename from emp""ConnectionString");
 
  DataTable dt = new DataTable();
  da.Fill(dt); // Fill the dataset
 
  DropDownList1.DataSource = dt;
  DropDownList1.DataValueField = "empid";
  DropDownList1.DataTextField = "ename";
  DropDownList1.DataBind();
}
Sri K replied to sunil pandey on 26-Jan-12 07:19 AM
We can bind the two columns to the dropdown 
Dictionary<string,string> dicts = new Dictionary<string,string>();
 
foreach(DataRow row in dt.Rows)
{
  string key = row["name"].ToString();
  string val = String.Format("{0}-{1}",row["name"],row["tagname"]);
}
then bind it to dropdown
 
dropdown.DataSource = dicts;
dropdown.DataValueField = "Key";
dropdown.DataTextField = "Value";
dropdown.DataBind();