how to use dataset for storing two column and displaying that column in dropdownlistbox

Answered how to use dataset for storing two column and displaying that column in dropdownlistbox

  • Thursday, May 10, 2012 12:34 PM
     
      Has Code

    Hi,

    i got the below code for the query"

    create a dataset with two column called customer name and id .using databound control to bind that and customer name and customer id should be displayed in two dropdown list box ...can anyone provide c# code for that"..

    this is the reply i got ,but

    DataTable table = new DataTable("Customer"); DataColumn id = new DataColumn("id"); DataColumn name = new DataColumn("name"); table.Columns.Add(id); table.Columns.Add(name); DataRow row = table.Rows.Add(); row["id"] = 1; row["name"] = "Adavesh"; row = table.Rows.Add(); row["id"] = 2; row["name"] = "John"; ... DataSet ds = new DataSet(); ds.Tables.Add(table); dropDownList.DataSource = ds.Tables[0]; dropDownList.DisplayMember = "name"; dropDownList.ValueMember = "id";

      



     

    but i am having probelm on following coding...

    in the above coding they created datatable and store the name and column but how to bind that name and id of that table  to dropdownlist1 and dropdownlist2...(in one dropdownbox i should get the "names" that are mention in that datatable and "id" on other dropdownlist box..)

    and after running if i select any name from dropdownlist1  box then corresponding id for that name should be displayed in dropdownlist2 box..can u provide c#code...

    thanks in advance. 








    sel.........

All Replies

  • Friday, May 11, 2012 5:08 AM
     
      Has Code

    Hi,

    Set the datasource property of both dropdrown list as mentione below

    dropDownList1.DataSource = ds.Tables[0];
    dropDownList1.DisplayMember = "id";
    dropDownList1.ValueMember = "id";
    // Set data Source of other drop down list
    dropDownList2.DataSource = ds.Tables[1];
    dropDownList2.DisplayMember = "id";
    dropDownList2.ValueMember = "name";
    Track the "OnSelectionIndexChanged" event of dropDownlist2 and on selection change set the dropdownlist1  "SelectedValue" Property from the selected Value of dropdownlist2.

    I hope this solves your querry. Please mark this post as answered if find its useful.

    Thanks and Regards
    Sharad Birajdar


    sharad

  • Friday, May 11, 2012 7:21 AM
    Moderator
     
     Answered

    Hi Sa,

    Is this the same issue to your this thread:http://social.msdn.microsoft.com/Forums/en-US/clr/thread/27249a2f-3161-48ec-8cf5-c02dad69f5a7/#81421028-d061-4cdd-b115-cccfde29f195 

    If so, please turn to that answered thread.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Sunday, May 13, 2012 5:47 AM
     
      Has Code

    Hi sa,

    Checkout this Mike's post:

    DataTable table = new DataTable("Customer");
            protected void Page_Load(object sender, EventArgs e)
            {
                DataColumn id = new DataColumn("id");
                DataColumn name = new DataColumn("name");
                table.Columns.Add(id);
                table.Columns.Add(name);
    
                DataRow row = table.Rows.Add();
                row["id"] = 1;
                row["name"] = "Adavesh";
    
                row = table.Rows.Add();
                row["id"] = 2;
                row["name"] = "John";
    
                DataSet ds = new DataSet();
                ds.Tables.Add(table);
    
                DropDownList1.DataSource = ds.Tables[0];
                DropDownList2.DataSource = DropDownList1.DataSource;
                DropDownList1.DataTextField = "name";
                DropDownList2.DataTextField = "id";
    
                DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged;
                DropDownList2.SelectedIndexChanged += DropDownList2_SelectedIndexChanged;
    
                DropDownList1.AutoPostBack = true;
                DropDownList2.AutoPostBack = true;
                if (!IsPostBack)
                {
                    DropDownList1.DataBind();
                    DropDownList2.DataBind();
                }
            }
    
            public void DropDownList1_SelectedIndexChanged(Object sender, EventArgs e)
            {
                DropDownList ddl = (DropDownList)sender;
                DropDownList2.Text = table.Rows[ddl.SelectedIndex][0].ToString();
            }
    
    
            public void DropDownList2_SelectedIndexChanged(Object sender, EventArgs e)
            {
                DropDownList ddl = (DropDownList)sender;
                DropDownList1.Text = table.Rows[ddl.SelectedIndex][1].ToString();
            }


    Regards, http://shwetamannjain.blogspot.com