Skip to main content

Filter in Asp.Net GridView with help of Dropdown

In this article I will explain filter GridView records with DropDownList Selection and TextBox using asp.net.

Step 1
Create ASP.NET Web Application project in Visual Studio. First of all, we will create Database to hold some data that we can display in a gridview. For this demo, I have created a Database with below fields and some dummy data.


                                                                                                    
Step 2
Now in the Default.aspx page, add a GridView, DropDownList, TextBox and two Button one for Search and Reset Aspx page layout will look like this
<table align="center">
            <tr>
                <td>
                    <asp:Label ForeColor="Red" Font-Bold="true" Font-Size="Large" Text="Demo to show how to search in grid using DropDown and textbox"
                        ID="lbltest" runat="server"></asp:Label>
                    <br />
                    <br />
                    <br />
                    <asp:DropDownList ID="ddlsearch" runat="server">
                        <asp:ListItem Text="Emp ID" Value="0">  </asp:ListItem>
                        <asp:ListItem Text="Department" Value="2">  </asp:ListItem>
                    </asp:DropDownList>
                    <asp:TextBox ID="txtsearch" runat="server"></asp:TextBox>
                    <asp:Button ID="btnsearch" runat="server" Text="Search" OnClick="btnsearch_Click" />
                    <asp:Button ID="btnreset" runat="server" Text="Reset" OnClick="btnreset_Click" />
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" PageSize="8"
                        AllowPaging="true" OnPageIndexChanging="GridView1_PageIndexChanging">
                        <Columns>
                            <asp:BoundField DataField="EmpiD" HeaderText="EmpiD" InsertVisible="False" ReadOnly="True"
                                SortExpression="EmpiD" />
                            <asp:BoundField DataField="Dob" HeaderText="Dob" InsertVisible="False" ReadOnly="True"
                                SortExpression="Dob" />
                            <asp:BoundField DataField="Empname" HeaderText="Empname" InsertVisible="False" ReadOnly="True"
                                SortExpression="Empname" />
                            <asp:BoundField DataField="Department" HeaderText="Department" InsertVisible="False"
                                ReadOnly="True" SortExpression="Department" />
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
        </table>

Step 3: Now in the Page_Load event, we will bind gridview to the dummy data. I have kept data in HttpContext.Current.Cache for this demo.

  if (!IsPostBack)
        {
            // method to bind grid
            GetData();
            // Remove cache at page load
            Cache.Remove("Saerchedresult");
        }

Step 4:  I have also created a class EmployEntities that we will use for creating a Generic list in which we will find items.



Step 5:  Now on search button Click we will code for Searching . I have kept all data in Cache and Created a List of it

       List<EmployEntities> newlist = new List<EmployEntities>();
        // Storedeatils is the name of cache
        newlist = Storedeatils;


Now we will use  a Linq Query to Saerch item in list that we have created this query will search for searchtext  in newlist 
  var r = (from i in newlist
                     where i.Department.ToLower().Contains(searchtext)
                     select i).ToList();

After searching is over we will bind Grid with searched result

                GridView1.DataSource = Saerchedresult;
                GridView1.DataBind();



Here don’t forgot to set the connection string in web.config file here I am getting database connection from web.config file for that reason you need to set the connectionstring in web.config file like this



You can Download Code from here

Comments

Post a Comment