How to set and apply the column filter in code behind for Silverlight GridData?
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 “Companyname” column 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
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
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”.