Searching GridView Data in ASP.NET

Published: Last Updated on 1.1K views 7 minutes read
A+A-
Reset

Hi,
Today, when visiting a community website, I found below article left unpublished with some comments about Language and Grammar. I am sharing this with you as it was. This was written on the initial days of year 2012 when I started learning .NET technology. This is basic one but I hope this is helpful to you.

Introduction

In this article, I am going to demonstrate, How to (Filter) Searching GridView data (outside Gridview) according to the selections in Dropdown List or any textbox control.

Objective

Sometime, we need to filter our Data, which is for record analyzing and study. So this is how we will filter our GridView Data according to any parameter passed from Page at run time.

We Are Using:

Visual Studio 2010, SQL Server 2008, XHTML 4.01, C# as Code Behind Language

What We Want:

Lets have a look at below Snapshots to Understand what we are going to do. We try to apply filter (searching) GridView.
GridView Data Before Clicking Filter Button with Filter Options:
searching gridview

Using the code

Here is code for ASPX Code.

<table width="100%">
 <tr>
 <td align="center" colspan="2">
 <h1>
 Prosperity Report</h1>
 </td>
 </tr>
 <tr>
 <td colspan="2" align="center">
 <table align="center" style="width: 74%" border="2">
 <tr>
 <td class="style2">
 Month:
 </td>
 <td class="style7">
 <asp:DropDownList ID="ddlMonths" runat="server">
 <asp:ListItem>April</asp:ListItem>
 <asp:ListItem>May</asp:ListItem>
 </asp:DropDownList>
 </td>
 <td class="style3">
 Mode:
 </td>
 <td class="style4">
 <asp:DropDownList ID="ddlModes" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlModes_SelectedIndexChanged">
 <asp:ListItem>Cargo</asp:ListItem>
 <asp:ListItem>Cargo-2</asp:ListItem>
 <asp:ListItem>Express</asp:ListItem>
 <asp:ListItem>Logistic</asp:ListItem>
 </asp:DropDownList>
 </td>
 <td class="style5">
 Party:
 </td>
 <td class="style6">
 <asp:DropDownList ID="ddlParties" runat="server">
 </asp:DropDownList>
 </td>
 <td class="style4">
 <asp:Button ID="btnFilter" runat="server" ToolTip="Click to Filter according to Selection"
 Text="Filter" OnClick="btnFilter_Click" />
 </td>
 </tr>
 </table>
 </td>
 </tr>
 <tr>
 <td colspan="2" align="center" runat="server">
 <asp:GridView ID="gvPros" runat="server" AutoGenerateColumns="false" Width="664px" Font-Names="Calibri"
 HeaderStyle-Font-Bold="true" AlternatingRowStyle-BackColor="LightGoldenrodYellow" EmptyDataText="No Records Found. Try again by Changing Filter Options.">
 <Columns>
 <asp:TemplateField HeaderText="Party Name">
 <ItemTemplate>
 <asp:Label ID="lblPartyName" runat="server" Text='<%#Eval("PartyName")%>'></asp:Label>
 </ItemTemplate>
 </asp:TemplateField>
 </Columns>
 <Columns>
 <asp:TemplateField HeaderText="Mode">
 <ItemTemplate>
 <asp:Label ID="lblFreight" runat="server" Text='<%#Eval("Division")%>'></asp:Label>
 </ItemTemplate>
 <EditItemTemplate>
 <asp:DropDownList ID="ddlDivision" runat="server"></asp:DropDownList>
 </EditItemTemplate>
 </asp:TemplateField>
 </Columns>
 <Columns>
 <asp:TemplateField HeaderText="Month">
 <ItemTemplate>
 <asp:Label ID="lblMonthName" runat="server" Text='<%#Eval("ProMonth")%>'></asp:Label>
 </ItemTemplate>
 </asp:TemplateField>
 </Columns>
 <Columns>
 <asp:TemplateField HeaderText="Total Box">
 <ItemTemplate>
 <asp:Label ID="lblTotalBox" runat="server" Text='<%#Eval("ProPkg")%>'></asp:Label>
 </ItemTemplate>
 </asp:TemplateField>
 </Columns>
 <Columns>
 <asp:TemplateField HeaderText="Actual Weight">
 <ItemTemplate>
 <asp:Label ID="lblPartyName" runat="server" Text='<%#Eval("ProActWt")%>'></asp:Label>
 </ItemTemplate>
 </asp:TemplateField>
 </Columns>
 <Columns>
 <asp:TemplateField HeaderText="Charged Wt">
 <ItemTemplate>
 <asp:Label ID="lblChWt" runat="server" Text='<%#Eval("ProChWt")%>'></asp:Label>
 </ItemTemplate>
 </asp:TemplateField>
 </Columns>
 <Columns>
 <asp:TemplateField HeaderText="Freight">
 <ItemTemplate>
 <asp:Label ID="lblFreight" runat="server" Text='<%#Eval("ProFreight")%>'></asp:Label>
 </ItemTemplate>
 </asp:TemplateField>
 </Columns>
 </asp:GridView>
 </td>
 </tr>
 <tr>
 <td align="center"><asp:LinkButton ID="lnkAddPros" runat="server"
 Text="Add Prosperity Record" onclick="lnkAddPros_Click"></asp:LinkButton></td>
 <td>
 <asp:LinkButton ID="lnkExport" runat="server" Text="Export to Excel"
 onclick="lnkExport_Click"></asp:LinkButton>
 </td>
 </tr>
 </table>
Now have a look at the code of complete .cs Page and what is behind the Page and Commands.
protected void Page_Load(object sender, EventArgs e)
 {
 if (!IsPostBack)
 {
 GVProsFill();
 ddlPartiesLoad();
 }
 }

public override void VerifyRenderingInServerForm(Control control)
 {

}

void GVProsFill()
 {
 string Query = "Select * from Parties, Prosperity where Parties.PartyID=Prosperity.ProParty";
 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConToJohn"].ConnectionString);
 SqlDataAdapter adp = new SqlDataAdapter(Query, con);
 DataSet ds = new DataSet();
 adp.Fill(ds);
 gvPros.DataSource = ds.Tables[0];
 gvPros.DataBind();
 }

void ddlPartiesLoad()
 {
 string Query = "Select * from Parties where Division='" + ddlModes.SelectedItem.ToString() + "'";
 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConToJohn"].ConnectionString);
 SqlDataAdapter adp = new SqlDataAdapter(Query, con);
 DataSet ds = new DataSet();
 adp.Fill(ds);
 ddlParties.DataSource = ds.Tables[0];
 ddlParties.DataTextField = "PartyName";
 ddlParties.DataValueField = "PartyID";
 ddlParties.DataBind();
 }
 protected void ddlModes_SelectedIndexChanged(object sender, EventArgs e)
 {
 ddlPartiesLoad();
 }
 protected void btnFilter_Click(object sender, EventArgs e)
 {
 string Query = "Select * from Parties, Prosperity where Parties.PartyID=Prosperity.ProParty and Division='" + ddlModes.SelectedItem.ToString() + "' and PartyName='" + ddlParties.SelectedItem.ToString() + "' and ProMonth='" + ddlMonths.SelectedItem.ToString() + "'";
 SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConToJohn"].ConnectionString);
 SqlDataAdapter adp = new SqlDataAdapter(Query, con);
 DataSet ds = new DataSet();
 try
 {
 adp.Fill(ds);
 gvPros.DataSource = ds.Tables[0];
 gvPros.DataBind();
 }
 catch (Exception ex)
 {
 throw ex;
 }
 finally
 {
 con.Dispose();
 adp.Dispose();
 ds.Dispose();
 }
 }
Now GridView Data After Clicking Filter Button (Applying filters):
searching gridview
Hope these snaps which would establish the implementation.

We used Dropdown Lists filled statically for Monthname and Mode as you can see in code. We Dynamically Bind Party names from Database.

Conclusion

After writing this code, I hope it will helpful to you. This code is running perfectly in my machine and you have applied searching GridView feature. You can enhance the code and design as per your design and requirement.

Related Posts

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Index

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.