Create a custom filter on WebGrid

Applies to: WebGrid 7.0
Published on: July 27, 2005 | Last Modified on August 1, 2011
Rate this topic:
     

Summary

This article describes how to create a custom filter on a WebGrid. To create a custom filter, a server-side event handler is already integrated inside the WebGrid, we call this the OnCustomFilter server-side event.

Content

WebGrid has a built-in filtering feature with multiple operations. However there are situations where these built-in operations are not sufficient.

WebGrid's server-side object model is built upon a rock-solid architecture, providing superior performance while maximizing ease of use. A highly granular approach to configuration allows you to set the properties of each element from the top level, leaving lower level properties at their default settings. If you prefer, you can also customize specific elements at deeper levels by simply changing their properties directly, without affecting other default settings.

This article contains a guide on how to do custom filtering by applying your own filter logic.

To achieve the current scenario the following steps must be applied:

  1. Preparing the WebGrid
  2. Implementing the OnCustomFilter server-side event

Preparing the WebGrid

Add the WebGrid instance to the WebForm and bind the data to the WebGrid. Then set the AllowFilter property to True, and set the AutomaticFilter property of the RootTable to False.

Implementing the OnCustomFilter server-side event

By using Regex, if the filterText of the filter column satisfies the Regex pattern, it will override the normal filtering process and implement your own filtering process. Add the OnCustomFilter server-side event, then add the following code:

protected void WebGrid1_CustomFilter(object sender, ISNet.WebUI.WebGrid.FilterEventArgs e)
{
    WebGridFilter filterCity = e.FilterColumns.GetNamedItem("City");
    if (filterCity != null)
    {
        Regex reg = new Regex("(.*)\\sOR\\s(.*)", RegexOptions.IgnoreCase);
        Match match = reg.Match(filterCity.FilterText);
        if (match.Success)
        {
            GroupCollection gc = match.Groups;
            e.FilterColumns.Remove(filterCity);
            String newFilter = "[City] LIKE '" + gc[1].Captures[0].Value + "' OR [City] LIKE '" + gc[2].Captures[0].Value + "'";
            e.FilterColumns.AdvancedFilterExpression = newFilter;
        }
    }
}

Type “Berlin Or London” in the filter text, and retrieve all records with their City column either Berlin Or London.

Files

KB_CustomFilter.rar

References

For more information about WebGrid and its features, see WebGrid Overview.

For more information about Client-side event in WebGrid, see Server-side Overview.

For additional How-to topics, Videos, Knowledge Base and other learning resources available for WebGrid, see WebGrid Support.

Note: This is a "QUICK PUBLISH" article created directly from within the Intersoft support organization. The information contained herein is provided as-is in response to emerging issues. As a result of the speed in making it available, the materials may include typographical errors and may be revised at any time without notice. See Terms of Use for other considerations.