Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASP.Net GridView Search with operators

Posted on 2015-01-03
5
Medium Priority
?
106 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 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 2000 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] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

782 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