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.
Here the Screenshot of Searching:-
GridView before search:
Search by Name:-
Search by Designation:-
The .aspx Code:-
<div style="margin:20px 0px 20px 20px">
<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();
}
}