What if I have created a INativeReport and I want to filter it, but the build in filters are not exactly what I need?
Well you can create your own filters! It does require some coding though (but we like code, right?).
As an example let’s add a “Region” filter to the “Customers Grouped by Region” report I created in my last post. To do this I need to create a new class that implements IReportFilterGenericV2 and ISerializable.
If you use Visual Studio to implement the IReportFilterGenericV2 interface you will notice a few sections. I’ll run through them quickly:
For the ISerializable implementation is pretty standard, the serialization is used when users save the report:
I have put a sample implementation of IReportFilterGenericV2 on this page if you would rather look at code…
With all of that work we now have a new filter that will show in the top filter bar in a report and in the filter dialog. To use it we have to add it to the report.
First we need to add the new filter to the report, to do that we add a few lines in the CreateFilters method:
// Add the region filter to the filter collection as a generic filter
RegionFilter regionFilter = new RegionFilter();
reportFilters.AddGenericFilter(regionFilter);
To actually use the filter we will need to apply the filter in the CreateData method. Naturally the implementation here will vary based on what you expect your filter to do, but I hope you get the idea from this sample:
if (reportFilters.ContainsFilter(ReportFilterEnum.GenericFilter))
{
RegionFilter reportRegionFilter
= reportFilters.GetFilter(ReportFilterEnum.GenericFilter)
as RegionFilter;
// apply region filter
if (String.IsNullOrEmpty(reportRegionFilter.State) == false)
if (filterString.Length > 0)
filterString.Append(" AND ");
filterString.Append("State = '");
filterString.Append(reportRegionFilter.State);
filterString.Append("'");
}
else if (reportRegionFilter.CensusRegion != (int)CensusRegions.All)
filterString.Append(
RegionFilter.GetCensusRegion(reportRegionFilter.CensusRegion));
// Apply row filter
view.RowFilter = filterString.ToString();
The result is a nice report with a custom filter: