Solved

ASP.Net GridView Search with operators

Posted on 2015-01-03
5
86 Views
Last Modified: 2015-01-06
I have a girdview in asp.net/C#. It displays data from a database. I'd like to allow users to search the different fields using < >, =, etc operators. Is there an good example of this? I have not been able to find anything as of yet. I know there are 3rd party tools I can use, but I am trying to avoid those. Thanks.
0
Comment
Question by:dodgerfan
  • 3
  • 2
5 Comments
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
There could be many sample depending how you are populating your gridview?
typed dataset, untyped dataset, LINQ, etc.

It will basically boil down to modify the adding or revising the query supplying data to your gv.

Give us more info.

Mike
0
 

Author Comment

by:dodgerfan
Comment Utility
I'm using VS 2008, C#. I am binding to an Access database, but will also be using it to search/filter gridviews that use a SQL server 2012 database.
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
Comment Utility
This doesn't help much. Could you show some code where you have your gv name and using its datasource property? Do you have .xsd file in your solution explorer?
0
 

Author Comment

by:dodgerfan
Comment Utility
Ok, here is my code so far. This is the asp.net page:
    <div style="margin-left: 25px; margin-top: 15px">
        <br />
        <table width="80%" align="center" cellpadding="3" border="0">
            <tr>
                <td align="center">
                    <asp:Label ID="title" runat="server" Text="Free Agent Batters" CssClass="title2" />
                </td>
            </tr>
            <tr>
                <td>
                    <div class="textEntry" style="text-align: center">
                        Select the position to filter players for.
                    </div>
                </td>
            </tr>
        </table>
        <table width="60%" align="center" cellpadding="3" border="1">
            <tr>
                <td align="left">
                    <asp:Label ID="lblPos" runat="server" Text="Select Position:" Font-Bold="True" Font-Names="Verdana"
                        Font-Size="Small"></asp:Label>
                    &nbsp;<asp:DropDownList ID="ddlPos" runat="server" AutoPostBack="True" Width="130px">
                        <asp:ListItem Value="1">Catchers</asp:ListItem>
                        <asp:ListItem Value="2">1st Base</asp:ListItem>
                        <asp:ListItem Value="3">2nd Base</asp:ListItem>
                        <asp:ListItem Value="4">3rd Base</asp:ListItem>
                        <asp:ListItem Value="5">Short Stop</asp:ListItem>
                        <asp:ListItem Value="8">Outfield</asp:ListItem>
                        <asp:ListItem Value="9">Designated Hitter</asp:ListItem>
                    </asp:DropDownList>
                    &nbsp;&nbsp;Or
                </td>
                <td>
                    <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
                    <asp:Button ID="cmdSearch" runat="server" Text="Search" OnClick="cmdSearch_Click" />
                </td>
                <td align="right">
                    <asp:Button ID="Pitchers" runat="server" OnClick="Pitchers_Click" Text="Pitchers"
                        CssClass="button" />
                </td>
            </tr>
            <tr>
                <td align="center" colspan="3">
                    <asp:GridView ID="Hitters" runat="server" AutoGenerateColumns="False" CssPostfix="PlasticBlue"
                        Width="100%" AllowPaging="true" PageSize="25" OnPageIndexChanging="Hitters_PageIndexChanging"
                        HeaderStyle-BackColor="CornflowerBlue" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White">
                        <Columns>
                            <asp:TemplateField HeaderText="Pos">
                                <ItemTemplate>
                                    <%# HighlightText(search_Word, (string)Eval("Pos"))%>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" Visible="false">
                                <ItemStyle Font-Size="14px" HorizontalAlign="Center" Font-Names="Calibri" />
                                <HeaderStyle Font-Names="Calibri" Font-Size="14px" />
                            </asp:BoundField>
                            <asp:TemplateField HeaderText="Name" HeaderStyle-Font-Names="Calibri" HeaderStyle-Font-Size="12px">
                                <ItemTemplate>
                                    <asp:HyperLink ID="player" NavigateUrl='<%# "javascript:openPopup(\"FAHitterInfo.aspx?id=" + Eval("id") + "\")" %>'
                                        runat="server" Text='<%# HighlightText(search_Word, (string)Eval("Name"))%>' Font-Names="Calibri" Font-Size="12px" />&nbsp;
                                    <a href="javascript:openInfo('injury.aspx?ID=<%# Eval("ID") %>')">
                                        <img id="img1" runat="server" src="~/Images/injury.png" alt="Click to view player injury data"
                                            visible='<%# Eval("Status") != DBNull.Value %>' /></a>&nbsp;
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="Team" HeaderText="Team" SortExpression="Team">
                                <ItemStyle Font-Size="14px" HorizontalAlign="Center" Font-Names="Calibri" />
                                <HeaderStyle Font-Names="Calibri" Font-Size="14px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="Games" HeaderText="Games" SortExpression="Games">
                                <ItemStyle Font-Size="14px" HorizontalAlign="Center" Font-Names="Calibri" />
                                <HeaderStyle Font-Names="Calibri" Font-Size="14px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="AB" HeaderText="AB" SortExpression="AB">
                                <ItemStyle Font-Size="14px" HorizontalAlign="Center" Font-Names="Calibri" />
                                <HeaderStyle Font-Names="Calibri" Font-Size="14px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="Hits" HeaderText="Hits" SortExpression="Hits">
                                <ItemStyle Font-Size="14px" HorizontalAlign="Center" Font-Names="Calibri" />
                                <HeaderStyle Font-Names="Calibri" Font-Size="14px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="Average" HeaderText="Average" SortExpression="Average">
                                <ItemStyle Font-Size="14px" HorizontalAlign="Center" Font-Names="Calibri" />
                                <HeaderStyle Font-Names="Calibri" Font-Size="14px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="Runs" HeaderText="Runs" SortExpression="Runs">
                                <ItemStyle Font-Size="14px" HorizontalAlign="Center" Font-Names="Calibri" />
                                <HeaderStyle Font-Names="Calibri" Font-Size="14px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="RBI" HeaderText="RBI" SortExpression="RBI">
                                <ItemStyle Font-Size="14px" HorizontalAlign="Center" Font-Names="Calibri" />
                                <HeaderStyle Font-Names="Calibri" Font-Size="14px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="HR" HeaderText="HR" SortExpression="HR">
                                <ItemStyle Font-Size="14px" HorizontalAlign="Center" Font-Names="Calibri" />
                                <HeaderStyle Font-Names="Calibri" Font-Size="14px" />
                            </asp:BoundField>
                            <asp:BoundField DataField="SB" HeaderText="SB" SortExpression="SB">
                                <ItemStyle Font-Size="14px" HorizontalAlign="Center" Font-Names="Calibri" />
                                <HeaderStyle Font-Names="Calibri" Font-Size="14px" />
                            </asp:BoundField>
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </div>

And the code behind:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class FreeAgents_FreeAgents : System.Web.UI.Page
{
    protected string search_Word = String.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            RefreshData();
    }

    private void RefreshData()
    {
        DataTable dt;
        String SQL = "SELECT * FROM vwFAHitters";


        string sConstr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        using (OleDbConnection conn = new OleDbConnection(sConstr))
        {
            using (OleDbCommand comm = new OleDbCommand(SQL, conn))
            {
                conn.Open();
                using (OleDbDataAdapter da = new OleDbDataAdapter(comm))
                {
                    dt = new DataTable("tbl");
                    da.Fill(dt);
                }
            }
        }

        Hitters.DataSource = dt;
        Hitters.DataBind();
    }
0
 
LVL 33

Accepted Solution

by:
Mike Eghtebas earned 500 total points
Comment Utility
It seems you have a table named vwFAHitters in your dataset. clik on .xsd file to view this table. In the lower portion of it (called table adapter), you need to add a search query (using wizard) which will accept your search parameters thus feeding filtered data to the gv.

To make sure this is how it is I wonder if you could make an image of  vwFAHitters in your dataset and post it here to see if you already have some queries in it.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now