Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to add items,bind (dataset) to second dropdown list on selection of 1st dropdown. i want to bind data dynamically to both dropdown
Posted
Updated 26-Nov-09 1:32am
v2

If you're using databinding, you can't add items manually.  Your best bet is to change both data sources to reflect the moved item, and then rebind.

 
Share this answer
 
under pageload write
C#
if (IsPostBack == false)
                 {

                     SqlCommand com = new SqlCommand("your query", con object);
                     SqlDataReader ddlValues;
                     con.Open();
                     ddlValues = com.ExecuteReader();

                     DropDownList1.DataSource = ddlValues;
                     DropDownList1.DataValueField = ".......";
                     DropDownList1.DataTextField = ".......";
                     DropDownList1.DataBind();
                     con.Close();
                 }

on dropdown1 select event write
C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    { SqlCommand com = new SqlCommand("query", con);
        
        SqlDataReader ddlValues;
        con.Open();
        ddlValues = com.ExecuteReader();

        DropDownList2.DataSource = ddlValues;
        DropDownList2.DataValueField = "column";
        DropDownList2.DataTextField = "column";
        DropDownList2.DataBind();
        con.Close();
}
 
Share this answer
 
v2
Comments
Sandeep Mewara 16-Feb-11 10:55am    
Use PRE tags to format code part

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