Solved

Filter a GridView with a search field

Posted on 2013-10-31
1
666 Views
Last Modified: 2013-11-01
I have a function GetEmployeeList that will list all records in the Employee Table. I use this data to populate a gridview. Now I need to search this gridview by the field fullname. So what I am trying to do is create a new function that will get the list generated by GetEmployeeList then based on the parameters filter the list based on the criteria. This way I can return the search criteria.
                        <asp:Button runat="server" ID="btnInsertEmployeeDetails" CssClass="CssEditEmployeeAdminPanel" Text="Create New Employee" Width="150px" OnClick="btnInsertEmployeeDetails_Click"/>
                        <asp:Label cssclass="CurvedTitleVED" ID="lblTitleVED" runat="server" Text="Select Employee"/>
                        <asp:CheckBox ID="chkShowInactiveRecords" runat="server" Text="Show Inactive Records" CssClass="CssChkBoxShowDisabled" OnCheckedChanged="chkShowInactiveRecords_CheckedChanged" Width="200px" AutoPostBack="true" Checked="True" />
                        <asp:GridView ID="grdEmpAdmin" OnRowCommand="grdEmpAdmin_RowCommand" runat="server" AutoGenerateColumns="False" DataSourceID="dsEmpObject" AllowPaging="True" PageSize="15" CssClass="CssZMGrid" CellPadding="3" GridLines="None" AllowSorting="false" BackColor="White" BorderColor="AliceBlue" BorderStyle="None" BorderWidth="0" CellSpacing="1" >
                            <Columns>
                                <asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="Left">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lnkSelectEmployee" runat="server" CommandName="cmdEmpSelect" CommandArgument='<%#Eval("EmployeeID")%>'>
                                            <asp:Image ID="imgSelectEmployee" runat="server" ImageUrl="~/images/MaintenanceGrid.png" BorderStyle="None" width="20" height="20"/>
                                        </asp:LinkButton>
                                        <asp:Image ID="imgRunning" runat="server" ImageUrl="~/images/running.gif" BorderStyle="None" width="20" height="20" Visible="false"/>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Full Name" SortExpression="FullName">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtFullName" runat="server" Text='<%# Bind("FullName") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblFullName" runat="server" Text='<%# Bind("FullName") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Employee Number" SortExpression="EmployeeNumber">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtEmployeeNumber" runat="server" Text='<%# Bind("EmployeeNumber") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblEmployeeNumber" runat="server" Text='<%# Bind("EmployeeNumber") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Department Name" SortExpression="DepartmentName">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtDepartmentName" runat="server" Text='<%# Bind("DepartmentName") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblDepartmentName" runat="server" Text='<%# Bind("DepartmentName") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Job Title Description" SortExpression="JobTitleDescription">
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtJobTitleDescription" runat="server" Text='<%# Bind("JobTitleDescription") %>'></asp:TextBox>
                                    </EditItemTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblJobTitleDescription" runat="server" Text='<%# Bind("JobTitleDescription") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                            <RowStyle BackColor="#EFF3FB" />
			                <AlternatingRowStyle BackColor="White" />
                            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#000000" />
                            <EditRowStyle BackColor="#2461BF" />
                            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                            <SortedAscendingCellStyle BackColor="#F5F7FB" />
                            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                            <SortedDescendingCellStyle BackColor="#E9EBEF" />
                            <SortedDescendingHeaderStyle BackColor="#4870BE" />
                        </asp:GridView>
                        <asp:ObjectDataSource ID="dsAssocObject" runat="server" SelectMethod="FilterAssociateList" TypeName="Psc.AssociateMaintenance.Factories.AssociateFactory">
                            <SelectParameters>
                                <asp:Parameter DefaultValue="1" Name="tSiteID" Type="Int32" />
                                <asp:Parameter DefaultValue="Jimmy" Name="FieldCriteria" Type="String" />
                            </SelectParameters>
                        </asp:ObjectDataSource>

Open in new window


//
        //--- Get Employee Records
        public List<Admin.wsEmployee> GetEmployeeList(out string strReturnMessage, int tLocationID, bool blDisabled)
        {
            List<Admin.wsEmployee> EmployeeAdminList = new List<Admin.wsEmployee>();

            try
            {
                EmployeeAdminList = _ws.GetEmployees(out strReturnMessage, tLocationID, blDisabled);
            }
            catch (Exception ex)
            {
                strReturnMessage = ex.Message.ToString();
            }
            return EmployeeAdminList;
        }
        //
        //--- Filter Employee List
        public List<Admin.wsEmployee> FilterEmployeeList(int tLocationID, string FieldCriteria)
        {
            string strReturnMessage = null;
            bool blDisabled = true;
            //string FilterEmployees = null;
            List<Admin.wsEmployee> GetList = new List<Admin.wsEmployee>();
            List<Admin.wsEmployee> FilterList = new List<Admin.wsEmployee>();
            try
            {
                GetList = GetEmployeeList(out strReturnMessage, tLocationID, blDisabled);

                FieldCriteria = "%" + FieldCriteria + "%";

                //List<Admin.wsEmployee> _filterdList=(from a in GetList where a.FirstName.Contains(FieldCriteria) || a.LastName.Contains(FieldCriteria) select a).ToList<Admin.wsEmployee>());

                //OR!!!!!!

                FilterList = GetList.Where(a => a.FullName == FieldCriteria).FirstOrDefault<Admin.wsEmployee>();

            }
            catch (Exception ex)
            {
                strReturnMessage = ex.Message.ToString();
            }
            return FilterList;
        }

Open in new window

0
Comment
Question by:RedXavier2k4
[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
1 Comment
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 39616070
I don't want to answer before saying the following
I don't know how many records we're talking here but this is highly inefficient.
You should query your database with your filter so that only the needed records are retrieved.

The way you're doing it is the shortest path to madness. You're not only wasting your database server, you're also wasting your application server. If they're both the same... it's even worse!

The thing is that you'll only really feel it after having a good user base and a good set of data and then you'll have a lot of work to re-do this logic.

Now for your answer
You can use string.Contains
You're filtering using LinqToObjects so %% notation is not valid.

This sample picks only the first record
FilterList = GetList.Where(a => a.FullName.Contains(FieldCriteria)).FirstOrDefault<Admin.wsEmployee>();

Open in new window

If you want to get a list
FilterList = GetList.Where(a => a.FullName.Contains(FieldCriteria)).ToList<Admin.wsEmployee>();

Open in new window

0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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