How to save and load filterbar values for all columns in WinForms GridGroupingControl?
Save and load filter bar values
In the 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#
StringCollection columnNames = new StringCollection();//Maintains all the column collection that are filtered. StringCollection conditions = new StringCollection();//Maintains filtercondition collection for columns that are filtered. //Saves recently performed Filter Operations. private void saveXML_Click(object sender, EventArgs e) { if (this.gridGroupingControl1.TableDescriptor.RecordFilters.Count != 0) { if (columnNames.Count != 0)//Overwrites the column names when save button is clicked more than once. { columnNames.Clear(); conditions.Clear(); for (int i = 0; i < this.gridGroupingControl1.TableDescriptor.RecordFilters.Count; i++) { columnNames.Add(this.gridGroupingControl1.TableDescriptor.RecordFilters[i].FieldDescriptor.Name);//Saves the filtered column names. conditions.Add(this.gridGroupingControl1.TableDescriptor.RecordFilters[i].Conditions[0].CompareText);//Saves filter condition for columns. } } else//Adds column names at first click. { for (int i = 0; i < this.gridGroupingControl1.TableDescriptor.RecordFilters.Count; i++) { columnNames.Add(this.gridGroupingControl1.TableDescriptor.RecordFilters[i].FieldDescriptor.Name);//Saves the filtered column names. conditions.Add(this.gridGroupingControl1.TableDescriptor.RecordFilters[i].Conditions[0].CompareText);//Saves the filter conditions for column. } } } private void LoadXML_Click(object sender, EventArgs e) { if (columnNames.Count != 0) { for (int i = 0; i < columnNames.Count; i++) //'Checks the columns that had been filtered. { RecordFilterDescriptor recFilt = new RecordFilterDescriptor(columnNames[i], new FilterCondition(FilterCompareOperator.Like, conditions[i])); //Defines record filter for the particular column with filter condition. this.gridGroupingControl1.TableDescriptor.RecordFilters.Add(recFilt);//Adds record filter for particular column with filter condition. } } }
VB
Private columnNames As New StringCollection() 'Maintains all the column collection that are filtered. Private conditions As New StringCollection() 'Maintains the filtercondition collection for columns that are all filtered. 'Saves recently performed Filter Operation. Private Sub saveXML_Click(ByVal sender As Object, ByVal e As EventArgs) If Me.gridGroupingControl1.TableDescriptor.RecordFilters.Count <> 0 Then If columnNames.Count <> 0 Then 'Overwrites the column names when the save button is clicked more than once. columnNames.Clear() conditions.Clear() For i As Integer = 0 To Me.gridGroupingControl1.TableDescriptor.RecordFilters.Count - columnNames.Add(Me.gridGroupingControl1.TableDescriptor.RecordFilters(i).FieldDescriptor.Name) 'Saves filter performed column names. conditions.Add(Me.gridGroupingControl1.TableDescriptor.RecordFilters(i).Conditions(0).CompareText) 'Saves filter condition for column. Next i Else 'Adds column names at first click. For i As Integer = 0 To Me.gridGroupingControl1.TableDescriptor.RecordFilters.Count - 1 columnNames.Add(Me.gridGroupingControl1.TableDescriptor.RecordFilters(i).FieldDescriptor.Name) 'Saves filter performed column names. conditions.Add(Me.gridGroupingControl1.TableDescriptor.RecordFilters(i).Conditions(0).CompareText) 'Saves filter condition for columns. Next i End If End If private void LoadXML_Click(Object sender, EventArgs e) If columnNames.Count <> 0 Then For i As Integer = 0 To columnNames.Count - 1 'Checks the columns that had been filtered. Dim recFilt As New RecordFilterDescriptor(columnNames(i), New FilterCondition(FilterCompareOperator.Like, conditions(i))) 'Defines record filter for the particular column filtered. Me.gridGroupingControl1.TableDescriptor.RecordFilters.Add(recFilt) 'Adds record filter for particular column filtered. Next i End If
In 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