Click here to Skip to main content
11,565,352 members (37,701 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET MySQL
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 11-Sep-12 4:31am
Ruwaldo733
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

We had something similar and we combined the two column values into one in query itself. Something like:
SELECT
  (FirstName + "," + Surname) AS Name
FROM
  MyTable
  Permalink  
Comments
Mohamed Mitwalli at 11-Sep-12 10:57am
   
5+
Ruwaldo at 13-Sep-12 10:17am
   
Thanks for this answer
Sandeep Mewara at 13-Sep-12 11:00am
   
Welcome.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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();
                }
            } 
        }
    }

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


Best Regards
M.Mitwalli
  Permalink  
Comments
Ruwaldo at 13-Sep-12 10:18am
   
Thanks for replying got it to work though
Mohamed Mitwalli at 13-Sep-12 11:10am
   
Your welcome :) good for you :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  

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

  Print Answers RSS


Advertise | Privacy | Mobile
Web04 | 2.8.150624.2 | Last Updated 11 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100