Click here to Skip to main content
15,936,677 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I want to bind two columns of dataset with a dropdownlist. Is It possible? If yes, then Please help me.How? This is my first question on this forum. It will be pleasure for me to find help by you through this forum.
Posted
Updated 25-Aug-11 22:37pm
v2
Comments
Suresh Suthar 26-Aug-11 4:40am    
What you want to do? You want to display one column as value field and other as text field or what? please elaborate.
thalavi 26-Aug-11 5:47am    
I want to display two columns such as""StundentName" + "StundentClass""of dataset in dropdownlist as a datatexfield
Herman<T>.Instance 26-Aug-11 4:41am    
only if you formatted the two columns to one column in your query

dropdownlist.DataSource = ds.Tables[0];
dropdownlist.DataTextField = "datacolumnname";
dropdownlist.DataValueField = "idfield";
dropdownlist.DataBind();
 
Share this answer
 
Comments
thalavi 29-Aug-11 1:30am    
Thank U to reply,
Actually I want to concatenate two columns of a table and want to display in dropdownlist e.g:
dropdownlist.DataSource = ds.Tables[0];
dropdownlist.DataTextField = "datacolumnname1"+"|"+"datacolumnname2";
dropdownlist.DataValueField = "idfield";
dropdownlist.DataBind();
but error occurs
Now did you clear about my problem.
Do you mean Datagrid?
Then see Multiple selection using DropDownList in DataGrid.

A dataset is a code object and I don't see any point in showing a dropdownlist in that.
 
Share this answer
 
try this one.
C#
protected void FillDrpEmpInfo()
   {
       dtTemp = new DataTable();
       dtTemp = CreateEmpDataTable();
       dtTemp.Columns.Add("Displaly", typeof(string), "ID + ' - ' + Name + ' ' + Surname");

       this.drpEmpInfo.DataSource = dtTemp;
       this.drpEmpInfo.DataTextField = "Displaly";
       this.drpEmpInfo.DataValueField = "ID";
       this.drpEmpInfo.DataBind();
   }

   public DataTable CreateEmpDataTable()
   {
       //Initialize DataTable
       dt = new DataTable();

       //Create columns in DataTable
       dt.Columns.Add("ID");
       dt.Columns.Add("Name");
       dt.Columns.Add("Surname");
       dt.Columns.Add("Salary");
       dt.Columns.Add("Designation");

       //Create Rows in DataTable
       dt.Rows.Add("1", "KiranKumar", "Roy", "5500", "Web Developer");
       dt.Rows.Add("2", "Hetvi", "Roy", "4500", "Web Developer");
       dt.Rows.Add("3", "Shankar", "Shetty", "7500", "Support Engg");
       dt.Rows.Add("4", "Loe", "Louis", "6500", "Support Engg");
       dt.Rows.Add("5", "Sanaj", "Meppery", "8000", "Sales Executive");
       dt.Rows.Add("6", "Jubair", "Mohd", "7000", "DNN Developer");
       dt.Rows.Add("7", "Sanjay", "Nadher", "5000", "CA");
       dt.Rows.Add("8", "Jyoti", "Ramwani", "3000", "Receptionist");
       dt.Rows.Add("9", "Sajid", "Shahid", "10000", "Sales and Techanical Manager");
       dt.Rows.Add("10", "Vijay", "Roy", "10000", "Glass Artist");

       return dt;
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900