Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to populate number of rows data in a gridview on selectedIndexChange event of dropdownList.?
i want a DDL with List Items in numbers and on selecting anyone the grid view gets populated from database.
Posted
Comments
db7uk 30-May-12 6:13am    
Can you not re-bind the grid on selected index changed? If you use a DataView you can / could filter your data and bind to the grid?
ujju.1 30-May-12 6:15am    
not clear. you have the codes for populating gridview?
just call it at selectedIndexChange event of the Ddl and do not forget to turn autopost= true for the event.

DropDownList1.SelectedValue will be the filter.
Just pass it in the query for Where Clause.
Then call the function to bind the data in the gridview.
 
Share this answer
 
Hi ,
Check this
XML
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
            onselectedindexchanged="DropDownList2_SelectedIndexChanged">
        </asp:DropDownList>

  <asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">       </asp:gridview>


C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (
             SqlConnection con =
                 new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("usp_Get_All", con);

                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adpt = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adpt.Fill(dt);
                DropDownList2.DataSource = dt;
                DropDownList2.DataTextField = "name";
                DropDownList2.DataValueField = "id";
                cmd.Dispose();
            }
        }
    }
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        using (
              SqlConnection con =
                  new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("usp_Search_All", con);

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("id", DropDownList2.SelectedValue);
            SqlDataAdapter adpt = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adpt.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            cmd.Dispose();
        }
    }

Best Regards
M.Mitwalli
 
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