Click here to Skip to main content
15,887,453 members
Articles / Programming Languages / C#

Real-time Filtering of a Grid or List

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
4 Jan 2016CPOL1 min read 19.8K   11   1
Real-time filtering of a grid or list

Have you ever looked at sites that have real-time filtering of a grid or list of products and wondered "how can I do that?" Maybe you're developing a few intranet apps that would do well having this type of functionality. Perhaps you’ve looked around for good tutorials with examples so you can understand the process but you’re not even sure where to start?

The key to implementing this kind of filtering is to apply it to the underlying data source rather than the presentation grid. I'm going to focus on one approach to filtering that relies on LINQ and some extension methods.

Let's assume for this example that you know where your data is coming from and how to get it. Maybe you're calling a stored procedure to get your data and populating your grid from that. Perhaps you're using an ORM. Either way, you have the data, which consists of credit reports that can be optionally filtered by type (Competitor/Customer/Vendor), year or company. You’ve made a call to your business service to retrieve your data into an enumerable of credit reports and now need to filter them.

C#
IEnumerable<CreditReport> allReports = businessService.GetAllCreditReports(); 	// retrieve from 
								// stored procedure/ORM or wherever 

The first filter we need is a filter by type. Add a BusinessServiceExtensions class to your project that looks like this:

C#
public static class BusinessServiceExtensions
{
  public static IEnumerable<CreditReport> FilteredByType(this IEnumerable<CreditReport> source, 
		ReportTypeOption reportType)
  {
    if (reportType == ReportTypeOption.None)
    {
      return source; // allows us to ignore type filtering if the user hasn’t selected a type
    }
    return source.Where(x => x.ReportType == reportType);
  }
}

We now need filtering for year and company. Add these 2 methods to your extensions class above:

C#
public static IEnumerable<CreditReport>
  FilteredByYear(this IEnumerable<CreditReport> source, int year)
{
  if (year == 0)
  {
    return source;
  }
  return source.Where(x => x.Year == year);
}

public static IEnumerable<CreditReport>
  FilteredByCompany(this IEnumerable<CreditReport> source, string company)
{
  if (string.IsNullOrEmpty(company))
  {
    return source;
  }
  return source.Where(x => x.Company == company);
}

We can now easily combine these extension methods using a fluent interface, because each one returns an IEnumerable<CreditReport>:

C#
return allReports
  .FilteredByType(reportType)
  .FilteredByYear(year)
  .FilteredByCompany(company);

View original article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Levelnis Ltd
United Kingdom United Kingdom
Follow along my journey as I create a newsletter about launching websites. Just message me with "I'm in" and I'll add you

Comments and Discussions

 
QuestionFiltering directly from database Pin
Tomaž Štih4-Jan-16 23:07
Tomaž Štih4-Jan-16 23:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.