Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have Two dropdowns and one gridview in my page,, i am binding one dropdownlist from database and the other i directly took list items in the page as there are only 5 items in another dropdown.

now the problem is i should be able to make the gridview respond to two dropdowns
in selectindex changed event and populate that in gridview... i am not getting how to do that..

onselectindex event not firing and data is not coming in gridview.

What I have tried:

<asp:DropDownList ID="ddlGroupName" runat="server" CssClass="form-control input-sm" AutoPostBack="true" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlGroup_SelectedIndexChanged">
                       <asp:ListItem Text="All" Value="0"></asp:ListItem>
                   </asp:DropDownList>



<div class="row">
    <div class="col-lg-12">
        <asp:GridView ID="grdDeviceDetails" runat="server" AutoGenerateColumns="false" CssClass="table table-hover table-bordered"
            AllowPaging="true" PagerSettings-PageButtonCount="3" GridLines="None" PageSize="10" OnPageIndexChanging = "OnPaging">
            <Columns>
                <asp:BoundField DataField="GroupName" HeaderText="GroupName" SortExpression="GroupName" />
                <asp:BoundField DataField="Brand" HeaderText="Brand" SortExpression="Brand" />
                <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
                <asp:BoundField DataField="SerialNo" HeaderText="SerialNo" SortExpression="SerialNo" />
                <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
                <asp:BoundField DataField="AddedOn" HeaderText="Date" SortExpression="AddedOn" />

            </Columns>
            <PagerStyle CssClass="pagination-ys" />
        </asp:GridView>
        </div>
    </div>


 <asp:DropDownList ID="ddlState" runat="server" CssClass="form-control input-sm" AutoPostBack="true" AppendDataBoundItems="true">
                  <asp:ListItem Text="All" Value="All"></asp:ListItem>
<%--                  <asp:ListItem Text="Active" Value="Active"></asp:ListItem>
                  <asp:ListItem Text="Assigned" Value="Assigned"></asp:ListItem>
                  <asp:ListItem Text="Returned" Value="Returned"></asp:ListItem>
                  <asp:ListItem Text="De-activated" Value="De-activated"></asp:ListItem>
                  <asp:ListItem Text="Sent for repair" Value="Sent for repair"></asp:ListItem>
                  <asp:ListItem Text="End of life-retired" Value="End of life-retired"></asp:ListItem>--%>
                  </asp:DropDownList>


public DataSet GetItems()
      {

          SqlConnection con = new SqlConnection(cs);
          SqlCommand cmd = new SqlCommand("sp_DeviceDetails", con);
          cmd.CommandType = CommandType.StoredProcedure;
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataSet ds = new DataSet();
          try
          {
              con.Open();
              da.Fill(ds);
              con.Close();
          }
          catch (Exception ex)
          {
              //write error message
          }
          return ds;
      }



        protected void Page_Load(object sender, EventArgs e)
        {
            ddlGroupName.DataSource = GetItems();
            ddlGroupName.DataTextField = "GroupName";
            ddlGroupName.DataBind();
            grdDeviceDetails.DataSource = GetItems();
            grdDeviceDetails.DataBind();
}


protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
{

        GetItems();
}
Posted
Updated 20-Feb-17 13:59pm
Comments
Mahesh2223 19-Feb-17 10:23am    
This is the full code.. two dropdowns and gridview.. one dropdown i have binded from database that is here GetItems().. now the problem is i have to make the gridview respond to two dropdowns.. for example if i select active in one dropdown i should get only active data in gridview, if i select any groupname in second dropdown i should get that data only..if i select in both dropdowns if there is matching data we should get or we should not get..how to do it.. can u please help me with the stored procedure to get the items just give me a hint like how to do
Mahesh2223 19-Feb-17 10:25am    
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{

GetStatus();
}

What do we write here to make the gridview respond to two dropdowns..
i have 5 items in 1 st drop down. and almost like 30 items in second dropdown

Add IsPostBack Condtion in PageLoad event

protected void Page_Load(object sender, EventArgs e)
         {
         if (!Page.IsPostBack)
           {
              ddlGroupName.DataSource = GetItems();
              ddlGroupName.DataTextField = "GroupName";
              ddlGroupName.DataBind();
              grdDeviceDetails.DataSource = GetItems();
              grdDeviceDetails.DataBind();
           }
         }
 
Share this answer
 
Comments
Mahesh2223 19-Feb-17 3:32am    
If i do that its showing error like groupname not present on the selected data source... and the problem is i should able to make gridview respond to dropdowns..
Karthik_Mahalingam 19-Feb-17 3:57am    
post the full code.
Mahesh2223 19-Feb-17 10:27am    
Reply Modify the comment. Delete the comment.
This is the full code.. two dropdowns and gridview.. one dropdown i have binded from database that is here GetItems().. now the problem is i have to make the gridview respond to two dropdowns.. for example if i select active in one dropdown i should get only active data in gridview, if i select any groupname in second dropdown i should get that data only..if i select in both dropdowns if there is matching data we should get or we should not get..how to do it.. can u please help me with the stored procedure to get the items just give me a hint like how to do
Mahesh2223 19-Feb-17 10:28am    
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{

GetStatus();
}

What do we write here to make the gridview respond to two dropdowns..
i have 5 items in 1 st drop down. and almost like 30 items in second dropdown
Karthik_Mahalingam 19-Feb-17 11:51am    
what is the need to call GetItems(); in the event..
only once in page load is enough
You need to pass the selected item from the DropDownList in your sql query or SP if you want to filter GridView based on the DropDownList selection. Here's a quick example for your reference:

<br />
protected void BindGrid(string selectedItem){<br />
    using(SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE")){<br />
                string sql = "SELECT Field1, Field2, Field3 FROM YourTableName WHERE YourFieldName = @Param1";<br />
        using(SqlCommand cmd = new SqlCommand(sql,connection)){<br />
                cmd.Parameters.AddWithValue("@Param1", selectedItem);<br />
<br />
                DataTable dt = new DataTable();<br />
                SqlDataAdapter ad = new SqlDataAdapter(cmd);<br />
                ad.Fill(dt);<br />
<br />
                if (dt.Rows.Count > 0) { //check if the query returns any data<br />
                       GridView1.DataSource = dt;<br />
                       GridView1.DataBind();<br />
                }<br />
                else<br />
                {<br />
                     //No records found<br />
                }<br />
        }<br />
<br />
    }<br />
}<br />


Then at SelectedIndexChanged event of your dropdown, you can call the method above like this:

<br />
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e){<br />
	 BindGrid(DropDownList1.SelectedItem.Text);<br />
}<br />


Just make sure to set AutoPostback to true for your dropdown to fire the selectedindexchanged event.
 
Share this answer
 
I am not an ASP.NET programmer (prefer MVC), but it appears that <asp:DropDownList ID="ddlState" runat="server" CssClass="form-control input-sm" AutoPostBack="true" AppendDataBoundItems="true"> control is not wired up for the OnSelectedIndexChanged event.
 
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