Category / Section
How to filter the search based on the typed text in WinForms GridGroupingControl?
2 mins read
Searching and filtering
You can filter the records in the grid according to the text that you have entered in the textbox outside the grid, by using the RecordFilterDescriptor class.
C#
this.textBoxExt1.TextChanged += new EventHandler(textBoxExt1_TextChanged); this.gridGroupingControl1.QueryCellStyleInfo += new GridTableCellStyleInfoEventHandler(gridGroupingControl1_QueryCellStyleInfo); void gridGroupingControl1_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e) { //Highlight the searching text. if (search) { if (e.Style.Text.Contains(this.textBoxExt1.Text) && e.TableCellIdentity.DisplayElement.Kind == DisplayElementKind.Record) e.Style.BackColor = Color.Orange; } } bool search = false; RecordFilterDescriptor recordFilter = new RecordFilterDescriptor(); void textBoxExt1_TextChanged(object sender, EventArgs e) { //Check whether the text is present or not. if (!string.IsNullOrEmpty(this.textBoxExt1.Text)) { recordFilter.Expression = "[CustomerName] Match " + "'" + this.textBoxExt1.Text + "'"; this.gridGroupingControl1.TableDescriptor.RecordFilters.Add(recordFilter); search = true; } else { this.gridGroupingControl1.TableDescriptor.RecordFilters.Clear(); search = false; } }
VB
Private Me.textBoxExt1.TextChanged += New EventHandler(AddressOf textBoxExt1_TextChanged) Private Me.gridGroupingControl1.QueryCellStyleInfo += New GridTableCellStyleInfoEventHandler(AddressOf gridGroupingControl1_QueryCellStyleInfo) Private Sub gridGroupingControl1_QueryCellStyleInfo(ByVal sender As Object, ByVal e As GridTableCellStyleInfoEventArgs) 'Highlight the searching text. If search Then If e.Style.Text.Contains(Me.textBoxExt1.Text) AndAlso e.TableCellIdentity.DisplayElement.Kind Is DisplayElementKind.Record Then e.Style.BackColor = Color.Orange End If End If End Sub Private search As Boolean = False Private recordFilter As New RecordFilterDescriptor() Private Sub textBoxExt1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) 'Check whether the text is present or not. If Not String.IsNullOrEmpty(Me.textBoxExt1.Text) Then recordFilter.Expression = "[CustomerName] Match " & "'" & Me.textBoxExt1.Text & "'" Me.gridGroupingControl1.TableDescriptor.RecordFilters.Add(recordFilter) search = True Else Me.gridGroupingControl1.TableDescriptor.RecordFilters.Clear() search = False End If End Sub
After applying the properties, the grid is displayed as follows,
Figure 1: Output
Samples: