How to save and load filterbar values for all columns in WinForms GridGroupingControl?
Save and load filter bar values
In the WinForms GridGroupingControl, it
is not possible to Serialize or Deserialize the filtering operations, by
default. But this can be achieved manually by manipulating it with the
collections. The previously performed filter in the Grid can be stored and
retrieved by using the RecordFilters property. In this case,
the previously performed filter can be stored by maintaining the collection for
column names and the FilterCondition of the columns that are
filtered (by using the Save button click). Then by using those
column names and filter conditions, the filter can be reassigned or loaded by
assigning this to the Record Filters.
C#
// Maintains all the column collection that are filtered
StringCollection columnNames = new StringCollection();
// Maintains filter condition collection for columns that are filtered
StringCollection conditions = new StringCollection();
// Saves recently performed Filter Operations
private void saveXML_Click(object sender, EventArgs e)
{
if (this.gridGroupingControl1.TableDescriptor.RecordFilters.Count != 0)
{
columnNames.Clear();
conditions.Clear();
for (int i = 0; i < this.gridGroupingControl1.TableDescriptor.RecordFilters.Count; i++)
{
// Saves the filtered column names
columnNames.Add(this.gridGroupingControl1.TableDescriptor.RecordFilters[i].FieldDescriptor.Name);
// Saves filter condition for columns
conditions.Add(this.gridGroupingControl1.TableDescriptor.RecordFilters[i].Conditions[0].CompareText);
}
}
}
private void LoadXML_Click(object sender, EventArgs e)
{
if (columnNames.Count != 0)
{
for (int i = 0; i < columnNames.Count; i++)
{
// Defines record filter for the particular column with filter condition
RecordFilterDescriptor recFilt = new RecordFilterDescriptor(
columnNames[i],
new FilterCondition(FilterCompareOperator.Like, conditions[i])
);
// Adds record filter for particular column with filter condition
this.gridGroupingControl1.TableDescriptor.RecordFilters.Add(recFilt);
}
}
}VB
'Maintains all the column collection that are filtered
Dim columnNames As New StringCollection()
'Maintains filter condition collection for columns that are filtered
Dim conditions As New StringCollection()
'Saves recently performed Filter Operations
Private Sub saveXML_Click(sender As Object, e As EventArgs)
If Me.gridGroupingControl1.TableDescriptor.RecordFilters.Count <> 0 Then
columnNames.Clear()
conditions.Clear()
For i As Integer = 0 To Me.gridGroupingControl1.TableDescriptor.RecordFilters.Count - 1
'Saves the filtered column names
columnNames.Add(Me.gridGroupingControl1.TableDescriptor.RecordFilters(i).FieldDescriptor.Name)
'Saves filter condition for columns
conditions.Add(Me.gridGroupingControl1.TableDescriptor.RecordFilters(i).Conditions(0).CompareText)
Next
End If
End Sub
Private Sub LoadXML_Click(sender As Object, e As EventArgs)
If columnNames.Count <> 0 Then
For i As Integer = 0 To columnNames.Count - 1
'Defines record filter for the particular column with filter condition
Dim recFilt As New RecordFilterDescriptor(
columnNames(i),
New FilterCondition(FilterCompareOperator.Like, conditions(i))
)
'Adds record filter for particular column with filter condition
Me.gridGroupingControl1.TableDescriptor.RecordFilters.Add(recFilt)
Next
End If
End SubIn the following image, the item France in the Country column is filtered, and the filtered values are saved by using the Save Filter button. Once the modification is saved, you can clear the filtering of the Country column. After that, load the saved filter state through the Load Filter button. Then the previously saved filter changes are retained.

Figure 1: France in Country column is filtered