?
Solved

Filter a GridView with a search field

Posted on 2013-10-31
1
Medium Priority
?
673 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 2000 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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for s…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

764 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