Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Real-time Filtering of a Grid or List

0.00/5 (No votes)
4 Jan 2016 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.

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:

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&rsquo;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:

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>:

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

View original article.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here