Solved

ASP.Net GridView Search with operators

Posted on 2015-01-03
5
88 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
ID: 40529299
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
ID: 40529310
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
ID: 40529313
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
ID: 40529650
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
ID: 40529677
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

939 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

10 Experts available now in Live!

Get 1:1 Help Now