In this blog, I just made a code to Search
GridView using the DropDownList Value and TextBox Text. DropDownList contains
two values, i.e Name and Designation, which is our Search Criteria. And TextBox will
generate the data as per the DropDownList Value.
<asp:Label ID="Label1" runat="server" Text="Search By" BorderColor="#99CC00"
BorderStyle="Solid" EnableTheming="True" ForeColor="#000066"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" style="margin-left: 27px">
<asp:ListItem>Name</asp:ListItem>
<asp:ListItem>Address</asp:ListItem>
<asp:ListItem>Designation</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtSearchName" runat="server" BackColor="#000066" style="margin-left:15px"
BorderStyle="Ridge" ForeColor="White"
></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Go" onclick="btnSearch_Click"
Width="65px" style="margin-left: 18px" />
</div>
The code to function the Data Search in GridView is as follows:-
protected void btnSearch_Click(object sender, EventArgs e)
{
string Query = string.Empty;
try
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
if (DropDownList1.SelectedValue.ToString() == "Name")
{
Query = "select * from tbl_Employee where Name Like '" + txtSearchName.Text + "%'";
}
else if (DropDownList1.SelectedValue.ToString() == "Designation")
{
Query = "select * from tbl_Employee where Designation Like '" + txtSearchName.Text + "%'";
}
SqlDataAdapter sqlDa = new SqlDataAdapter(Query, sqlCon);
DataSet Ds = new DataSet();
sqlDa.Fill(Ds);
dgvEmployee.DataSource = Ds;
dgvEmployee.DataBind();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write("<script>alert('wfrmGrid: 11')</script>" + ex.Message);
}
finally
{
sqlCon.Close();
}
}

allanabrenica abrenicar5Posted Nov 1, 2018, 5:31 AM
Hello what if i want to filter gridview by date? thank you
Niket SoniPosted Jul 4, 2016, 1:32 AM
How can i use this code in my mvc5 Project??
Hannah MugurePosted Mar 19, 2014, 3:42 AM
how do you implement the same code using stored procedure?