Solved

Filter a GridView with a search field

Posted on 2013-10-31
1
640 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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 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

18 Experts available now in Live!

Get 1:1 Help Now