Solved

Filter a GridView with a search field

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Aspx calendar pop up control 3 37
Nested forach loop to linq 3 42
Broken .resx file generating errors 18 44
Help solve error message when converting date using vb.NET 10 42
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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

732 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