Articles in this section
Category / Section

How to set and apply the column filter in code behind for WPF GridDataControl?

2 mins read

WPF GridDataControl allows you to create filters and apply them from code behind in two ways,

  • Using Column filter
  • Using View filter

Column filter:

You can apply filter to a particular column by adding FilterPredicate to the Filters of that VisibleColumn using Filters.Add method.

FilterPredicate class has the following properties.

 

Property

Type

Descriptions

FilterBehavior

FilterBehavior

Determines the filter behavior of the predicate.

FilterType

FilterType

Determines the type of filter to be applied for the predicate.

FilterValue

Object

Determines the filter value to be applied for the predicate.

IsCaseSensitive

Boolean

Determines whether the predicate to be applied with the filter value is case sensitive.

PredicateType

PredicateType

Determines the relations of multiple predicates and whether they should be applied with OR or AND.




 

Filter Behavior

  • StringTyped: Records are filtered without considering the type and it takes all the types as string.
  • StronglyTyped: Records are filtered according to the underlying type.

 

Filter Type

  • Contains: Checks the records that contain the filter value.
  • EndsWith: Checks the records which end with the filter value.
  • Equals: Checks the records which are equal to the filter value.
  • GreaterThan: Checks the records which are greater than the filter value.
  • GreaterThanOrEqual: Checks the records which are greater than or equal to the filter value.
  • LessThan: Checks the records which are less than the filter value.
  • LessThanOrEqual: Checks the records which are less than or equal to the filter value.
  • NotEquals: Checks the records which are not equal to the filter value.
  • StartsWith: Checks the records which start with the filter value.
  • Between: Checks the records which have values between the filter values.

 

Predicate Type

  • AND: Applies AND operator between the predicates.
  • OR: Applies OR operator between the predicates.

 

You can set any Predicate Type and conditions available in Predicate class to a VisibleColumn’s Filter. Refer the following code example, where column filter is applied to the “Companynamecolumn with FilterType “Equals”, FilterValue Syncfusion”, and PredicateType as And”.

C#

void ColumnFilter_Click(object sender, RoutedEventArgs e)
{
    //adding filter to the column “Companyname”
    this.dataGrid.VisibleColumns["Companyname"].Filters.Add(new FilterPredicate()
    {
        FilterType = FilterType.Equals,
        FilterValue = "Syncfusion",
        PredicateType = PredicateType.And,
        FilterBehavior = FilterBehavior.StronglyTyped
    });
}

 

Following snapshots illustrates the effect of above code in GridDataControl

D:\Syncfusion\Issues\2014 Volume 2\KB Issues\KB Images\Filtering.PNG

 

View Filters:

GridDataControl provides support to achieve view filtering by setting the GridDataControl.Model.View.Filter property. FilterRecords method filters the data that contains the filterText value. To filter a column assign, FilterRecords method to GridDataControl.Model.View.Filter predicate. Refer the following code.

C#

void ViewFilter_Click(object sender, RoutedEventArgs e)
{
    this.dataGrid.Model.View.Filter = FilterRecords;
    this.dataGrid.Model.View.RefreshFilters();
}
 
//View filtering 
private bool FilterRecords(object obj)
{
     //getting string from a text box
    string filterText = this.FilterBox.Text.ToString();
    var item = obj as Employee;
    if (item != null)
    {
        if (this.FilterBox.Text.ToString() != string.Empty)
        {
            if (filterText.Contains(","))
            {
                //spliiting the search string and filtering 
                var ids = filterText.Split(',');
                foreach (var id in ids)
                {
                    if (item.EmpName.Contains(id))
                        return true;
                }
            }
            else
            {
                if (item.EmpName.Contains(filterText))
                    return true;
            }
        }
    }
    return false;
}

 

Following snapshot illustrates the effect of above code in GridDataControl

 

D:\Syncfusion\Issues\2014 Volume 2\KB Issues\KB Images\ViewFilter.PNG

 

In this example, the GridDataControl contains EmployeeDetails as ItemsSource. FilterRecords method is used to filter the filterText (filterText is the text entered in the text box in the view). FilterRecords is assigned to dataGrid.Model.View.Filter predicate to filter the “EmpName” column. Here the EmpName column is filtered using a search string “raj”.


Conclusion

I hope you enjoyed learning about how to set and apply the column filter in code behind for WPF GridDataControl.


You can refer to our WPF GridDataControl feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF GridDataControl example
to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied