Solved

ASP.Net GridView Search with operators

Posted on 2015-01-03
5
99 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 34

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 34

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 34

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

630 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