Solved

Filter a GridView with a search field

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

786 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