Solved

ASP.Net GridView Search with operators

Posted on 2015-01-03
5
95 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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!
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 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