Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, I am stuck with a little problem. I have a table in the database with two columns "name" and "surname" and i have a dropdownlist. I need to have the dropdownlists datatextfield contain both the name and the surname when i databind. Any help would be much appreciated. Thanking you in advance
Posted

We had something similar and we combined the two column values into one in query itself. Something like:
SQL
SELECT
  (FirstName + "," + Surname) AS Name
FROM
  MyTable
 
Share this answer
 
Comments
Mohamed Mitwalli 11-Sep-12 10:57am    
5+
Ruwaldo 13-Sep-12 10:17am    
Thanks for this answer
Sandeep Mewara 13-Sep-12 11:00am    
Welcome.
Hi ,
Check this
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
            {
                con.Open();
                   //Change with your select statement .
                using (SqlCommand cmd = new SqlCommand("select * from test1", con))
                {
                    DataTable dt = new DataTable();
                    SqlDataAdapter adpt = new SqlDataAdapter(cmd);
                    adpt.Fill(dt);
                    Dictionary<int,string> lst = new Dictionary<int,string>();
                    foreach (DataRow row in dt.Rows)
                    {
                        //Add values to Dictionary
                        string val = row[1].ToString() + " , " + row[2].ToString() + " , " + row[3].ToString();
                        lst.Add(Convert.ToInt32(row[0]), val);
                    }
                    DropDownList1.DataSource = lst;
                    DropDownList1.DataTextField = "Value";
                    DropDownList1.DataValueField = "Key";
                    DropDownList1.DataBind();
                }
            } 
        }
    }


XML
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>



Best Regards
M.Mitwalli
 
Share this answer
 
Comments
Ruwaldo 13-Sep-12 10:18am    
Thanks for replying got it to work though
Mohamed Mitwalli 13-Sep-12 11:10am    
Your welcome :) good for you :)
You can do this also by building a List for Merged Columns and then bind dropdown from list<> or a nice post is here...

http://stackoverflow.com/questions/9275096/merge-2-columns-from-datatable-in-datatextfield-from-dropdownlist
 
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