GridView With Multiple Filters

In this article, you will learn to apply multiple filter in GridView
 
Let's start.
 
Step 1 - Add New Project
 
Open Visual Studio 2017 => Click Add Project => Select Web Application Template => Fill all required details.
 
GridView With Multiple Filters
 
Step 2
Select Web Application Template.
 
GridView With Multiple Filters
 
Step 3
 
Add new item.

Right click in project solution => Add => New Item => Select Web Form = > DEMO.aspx
 
GridView With Multiple Filters
 
Step 4
 
Paste the below Html code under the body tag.
  1. <form id="form1" runat="server">  
  2.         <center><h1>Gridview With Multiple Filter</h1></center>  
  3.         <div style="justify-content: center; display: flex">  
  4.             <table style="text-align: center;">  
  5.                 <tr>  
  6.                     <td style="border-left: 1px solid #dddddd; border-right: 1px solid #dddddd; border-top: 1px solid #dddddd; border-bottom: 1px solid #dddddd; padding-left: 22px; padding-right: 22px;">  
  7.                         <asp:DropDownList ID="ddlteamfilter" runat="server" OnSelectedIndexChanged="OnSelectedIndexChanged_ddlteamfilter" AutoPostBack="true">  
  8.                             <asp:ListItem Text="ALL" Value="ALL"></asp:ListItem>  
  9.                             <asp:ListItem Text="Development" Value="Development"></asp:ListItem>  
  10.                             <asp:ListItem Text="UAT" Value="UAT"></asp:ListItem>  
  11.                             <asp:ListItem Text="Product" Value="Product"></asp:ListItem>  
  12.                             <asp:ListItem Text="Testing" Value="Testing"></asp:ListItem>  
  13.                             <asp:ListItem Text="Deployment" Value="Deployment"></asp:ListItem>  
  14.                         </asp:DropDownList>  
  15.                     </td>  
  16.                     <td style="border-left: 1px solid #dddddd; border-right: 1px solid #dddddd; border-top: 1px solid #dddddd; border-bottom: 1px solid #dddddd; padding-left: 22px; padding-right: 22px;">  
  17.                         <asp:TextBox ID="txtLocationfilter" runat="server" OnTextChanged="txtLocationfilter_TextChanged"  
  18.                             placeholder="Enter Location" AutoPostBack="true"></asp:TextBox>  
  19.                     </td>  
  20.                     <td style="border-left: 1px solid #dddddd; border-right: 1px solid #dddddd; border-top: 1px solid #dddddd; border-bottom: 1px solid #dddddd; padding-left: 22px; padding-right: 22px;">  
  21.                         <asp:TextBox ID="txtEmployeefilter" runat="server" OnTextChanged="txtEmployeefilter_TextChanged"  
  22.                             placeholder="Enter Employee" AutoPostBack="true"></asp:TextBox>  
  23.                     </td>  
  24.                 </tr>  
  25.             </table>  
  26.         </div>  
  27.         <hr />          
  28.         <br />  
  29.         <div style="justify-content: center; display: flex">  
  30.             <asp:GridView ID="gvEmployee" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">  
  31.                 <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />  
  32.                 <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />  
  33.                 <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />  
  34.                 <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />  
  35.                 <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />  
  36.                 <SortedAscendingCellStyle BackColor="#FFF1D4" />  
  37.                 <SortedAscendingHeaderStyle BackColor="#B95C30" />  
  38.                 <SortedDescendingCellStyle BackColor="#F1E5CE" />  
  39.                 <SortedDescendingHeaderStyle BackColor="#93451F" />  
  40.   
  41.             </asp:GridView>  
  42.         </div>  
  43.     </form>  
Step 5
 
Replace auto generated class code with the below code.
 
Note
Here I am hardcoded Employee data in class file , you can use Database. 
  1. public partial class DEMO : System.Web.UI.Page    
  2. {    
  3.     protected void Page_Load(object sender, EventArgs e)    
  4.     {    
  5.         if (!IsPostBack)    
  6.         {    
  7.             BindEmployeeGridView();    
  8.         }    
  9.     }    
  10.   
  11.     public void BindEmployeeGridView()    
  12.     {    
  13.         DataTable dtEmployee = new DataTable();    
  14.         dtEmployee.Columns.Add("EmployeeID"typeof(String));    
  15.         dtEmployee.Columns.Add("Name"typeof(String));    
  16.         dtEmployee.Columns.Add("Gender"typeof(String));    
  17.         dtEmployee.Columns.Add("Team"typeof(String));    
  18.         dtEmployee.Columns.Add("Location"typeof(String));    
  19.         DataRow dr1 = dtEmployee.NewRow();    
  20.         dr1["EmployeeID"] = "EMP1";    
  21.         dr1["Name"] = "Mahesh Vishwakarma";    
  22.         dr1["Gender"] = "Male";    
  23.         dr1["Team"] = "Development";    
  24.         dr1["Location"] = "Mumbai";    
  25.         DataRow dr2 = dtEmployee.NewRow();    
  26.         dr2["EmployeeID"] = "EMP2";    
  27.         dr2["Name"] = "Srihari Ausakar";    
  28.         dr2["Gender"] = "Male";    
  29.         dr2["Team"] = "Development";    
  30.         dr2["Location"] = "Noida";    
  31.         DataRow dr3 = dtEmployee.NewRow();    
  32.         dr3["EmployeeID"] = "EMP3";    
  33.         dr3["Name"] = "Amit Berad";    
  34.         dr3["Gender"] = "Male";    
  35.         dr3["Team"] = "Development";    
  36.         dr3["Location"] = "Mumbai";    
  37.         DataRow dr4 = dtEmployee.NewRow();    
  38.         dr4["EmployeeID"] = "EMP4";    
  39.         dr4["Name"] = "Kishor V";    
  40.         dr4["Gender"] = "Male";    
  41.         dr4["Team"] = "UAT";    
  42.         dr4["Location"] = "Pune";    
  43.         DataRow dr5 = dtEmployee.NewRow();    
  44.         dr5["EmployeeID"] = "EMP5";    
  45.         dr5["Name"] = "Suraj Keni";    
  46.         dr5["Gender"] = "Male";    
  47.         dr5["Team"] = "UAT";    
  48.         dr5["Location"] = "Pune";    
  49.         DataRow dr6 = dtEmployee.NewRow();    
  50.         dr6["EmployeeID"] = "EMP6";    
  51.         dr6["Name"] = "Darshan";    
  52.         dr6["Gender"] = "Male";    
  53.         dr6["Team"] = "Product";    
  54.         dr6["Location"] = "Bangalore";    
  55.         DataRow dr7 = dtEmployee.NewRow();    
  56.         dr7["EmployeeID"] = "EMP7";    
  57.         dr7["Name"] = "Vandana";    
  58.         dr7["Gender"] = "Female";    
  59.         dr7["Team"] = "Testing";    
  60.         dr7["Location"] = "Mumbai";    
  61.         DataRow dr8 = dtEmployee.NewRow();    
  62.         dr8["EmployeeID"] = "EMP8";    
  63.         dr8["Name"] = "Sachin";    
  64.         dr8["Gender"] = "Male";    
  65.         dr8["Team"] = "Deployment";    
  66.         dr8["Location"] = "Mumbai";    
  67.         DataRow dr9 = dtEmployee.NewRow();    
  68.         dr9["EmployeeID"] = "EMP9";    
  69.         dr9["Name"] = "Radhika";    
  70.         dr9["Gender"] = "Female";    
  71.         dr9["Team"] = "Deployment";    
  72.         dr9["Location"] = "Mumbai";    
  73.         DataRow dr10 = dtEmployee.NewRow();    
  74.         dr10["EmployeeID"] = "EMP10";    
  75.         dr10["Name"] = "Swanit Sankpal";    
  76.         dr10["Gender"] = "Male";    
  77.         dr10["Team"] = "Development";    
  78.         dr10["Location"] = "Pune";    
  79.         dtEmployee.Rows.Add(dr1);    
  80.         dtEmployee.Rows.Add(dr2);    
  81.         dtEmployee.Rows.Add(dr3);    
  82.         dtEmployee.Rows.Add(dr4);    
  83.         dtEmployee.Rows.Add(dr5);    
  84.         dtEmployee.Rows.Add(dr6);    
  85.         dtEmployee.Rows.Add(dr7);    
  86.         dtEmployee.Rows.Add(dr8);    
  87.         dtEmployee.Rows.Add(dr9);    
  88.         dtEmployee.Rows.Add(dr10);    
  89.         gvEmployee.DataSource = dtEmployee;    
  90.         gvEmployee.DataBind();    
  91.     }    
  92.   
  93.     protected void OnSelectedIndexChanged_ddlteamfilter(object sender, EventArgs e)    
  94.     {    
  95.         BindEmployeeGridView();    
  96.         if (txtLocationfilter.Text != "" && txtEmployeefilter.Text != "")    
  97.         {    
  98.             (gvEmployee.DataSource as DataTable).DefaultView.RowFilter = string.Format(ddlteamfilter.SelectedValue == "ALL" ? "(Location LIKE '{0}%' OR Location LIKE '%{0}%') OR (Team = '{1}') AND (Name LIKE '{2}%' OR Name LIKE '%{2}%')" : "(Location LIKE '{0}%' OR Location LIKE '%{0}%') AND Team = '{1}' AND (Name LIKE '{1}%' OR Name LIKE '%{1}%')", txtEmployeefilter.Text, ddlteamfilter.SelectedValue == "ALL" ? "" : ddlteamfilter.SelectedValue, txtLocationfilter.Text);                    
  99.         }    
  100.         else if (txtEmployeefilter.Text != "")    
  101.         {    
  102.             (gvEmployee.DataSource as DataTable).DefaultView.RowFilter = string.Format(ddlteamfilter.SelectedValue == "ALL" ? "(Name LIKE '{1}%' OR Name LIKE '%{1}%') OR Team = '{1}'" : "(Name LIKE '{1}%' OR Name LIKE '%{1}%') AND Team = '{1}'", txtEmployeefilter.Text, ddlteamfilter.SelectedValue == "ALL" ? "" : ddlteamfilter.SelectedValue);                    
  103.         }    
  104.         else if (txtLocationfilter.Text != "")    
  105.         {    
  106.             (gvEmployee.DataSource as DataTable).DefaultView.RowFilter = string.Format(ddlteamfilter.SelectedValue == "ALL" ? "(Location LIKE '{0}%' OR Location LIKE '%{0}%') OR Team = '{1}'" : "(Location LIKE '{0}%' OR Location LIKE '%{0}%') AND Team = '{1}'", txtLocationfilter.Text, ddlteamfilter.SelectedValue == "ALL" ? "" : ddlteamfilter.SelectedValue);                    
  107.         }    
  108.         else    
  109.         {    
  110.             if (ddlteamfilter.SelectedValue != "ALL")    
  111.             {    
  112.                 (gvEmployee.DataSource as DataTable).DefaultView.RowFilter = string.Format("Team = '{0}'", ddlteamfilter.SelectedValue);    
  113.             }    
  114.         }   
  115.         gvEmployee.DataBind();  
  116.     }    
  117.   
  118.     protected void txtLocationfilter_TextChanged(object sender, EventArgs e)    
  119.     {    
  120.         BindEmployeeGridView();    
  121.         if (txtEmployeefilter.Text != "" && ddlteamfilter.SelectedValue != "ALL")    
  122.         {    
  123.             (gvEmployee.DataSource as DataTable).DefaultView.RowFilter = string.Format("(Location LIKE '{0}%' OR Location LIKE '%{0}%') AND Team = '{1}' AND (Name LIKE '{2}%' OR Name LIKE '%{2}%')", txtLocationfilter.Text, ddlteamfilter.SelectedValue, txtEmployeefilter.Text);    
  124.         }    
  125.         else if (ddlteamfilter.SelectedValue != "ALL")    
  126.         {    
  127.             (gvEmployee.DataSource as DataTable).DefaultView.RowFilter = string.Format(" Team = '{0}' AND (Location LIKE '{1}%' OR Location LIKE '%{1}%')", ddlteamfilter.SelectedValue, txtLocationfilter.Text);    
  128.         }    
  129.         else if (txtEmployeefilter.Text != "")    
  130.         {    
  131.             (gvEmployee.DataSource as DataTable).DefaultView.RowFilter = string.Format("(Location LIKE '{0}%' OR Location LIKE '%{0}%') AND (Name LIKE '{1}%' OR Name LIKE '%{1}%')", txtLocationfilter.Text, txtEmployeefilter.Text);    
  132.         }    
  133.         else    
  134.         {    
  135.             (gvEmployee.DataSource as DataTable).DefaultView.RowFilter = string.Format("Location LIKE '{0}%' OR Location LIKE '%{0}%'", txtLocationfilter.Text);    
  136.         }  
  137.         gvEmployee.DataBind();    
  138.   
  139.     }    
  140.     protected void txtEmployeefilter_TextChanged(object sender, EventArgs e)    
  141.     {  
  142.         BindEmployeeGridView();  
  143.         if (txtLocationfilter.Text != "" && ddlteamfilter.SelectedValue != "ALL")    
  144.         {    
  145.             (gvEmployee.DataSource as DataTable).DefaultView.RowFilter = string.Format("(Location LIKE '{0}%' OR Location LIKE '%{0}%') AND Team = '{1}' AND (Name LIKE '{2}%' OR Name LIKE '%{2}%')", txtLocationfilter.Text, ddlteamfilter.SelectedValue, txtEmployeefilter.Text);    
  146.         }    
  147.         else if (ddlteamfilter.SelectedValue != "ALL")    
  148.         {    
  149.             (gvEmployee.DataSource as DataTable).DefaultView.RowFilter = string.Format(" Team = '{0}' AND (Name LIKE '{1}%' OR Name LIKE '%{1}%')", ddlteamfilter.SelectedValue, txtEmployeefilter.Text);    
  150.         }    
  151.         else if (txtLocationfilter.Text != "")    
  152.         {    
  153.             (gvEmployee.DataSource as DataTable).DefaultView.RowFilter = string.Format("(Location LIKE '{0}%' OR Location LIKE '%{0}%') AND (Name LIKE '{1}%' OR Name LIKE '%{1}%')", txtLocationfilter.Text, txtEmployeefilter.Text);    
  154.         }    
  155.         else    
  156.         {    
  157.             (gvEmployee.DataSource as DataTable).DefaultView.RowFilter = string.Format("Name LIKE '{0}%' OR Name LIKE '%{0}%'", txtEmployeefilter.Text);    
  158.         }  
  159.         gvEmployee.DataBind();    
  160.     }  
  161. } 
Step 5
 
Run application.
 
GridView With Multiple Filters
 
Filter With Team
 
GridView With Multiple Filters
 
Now Apply Location Filter
 
GridView With Multiple Filters
 
Now Apply EmployeeName Filter
 
GridView With Multiple Filters
 
Apply Only Location Filter
 
GridView With Multiple Filters
 
Apply Only EmployeeName Filter
 
GridView With Multiple Filters
 
I hope this helps.
 
Thank you.