Back to Devexpress

ColumnView.ParseFindPanelText Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-columnview-2f1fde3e.md

latest10.3 KB
Original Source

ColumnView.ParseFindPanelText Event

Fires after the query in the find panel changes. Allows you to create a filter condition based on the query and specify how to highlight results in the control.

Namespace : DevExpress.XtraGrid.Views.Base

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Data")]
public event EventHandler<ParseFindPanelTextEventArgs> ParseFindPanelText
vb
<DXCategory("Data")>
Public Event ParseFindPanelText As EventHandler(Of ParseFindPanelTextEventArgs)

Event Data

The ParseFindPanelText event's data class is DevExpress.Data.ParseFindPanelTextEventArgs.

Remarks

The FindPanelText event argument returns the query in the find panel. Based on the query, you can create a CriteriaOperator object that specifies a filter condition. To apply the condition to the control, use the following methods:

  • SetFindCriteria - applies the specified filter condition.
  • SetFindCriteriaAndHighlightedRangesGetterFromDisplayText - allows you to provide a function that highlights results in the control. The function accepts the text displayed in the processed cell and returns positions that should be highlighted.
  • SetFindCriteriaAndHighlightedRangesGetterFromDisplayTextAndFieldName - the function accepts the text displayed in the processed cell and the field name.

The Handled event argument should be set to true to prevent the default filter algorithm from being invoked.

Examples

The code below shows how to use a comma to split the search query into several queries.

csharp
using DevExpress.Data;
using DevExpress.Data.Filtering;

private void PropertyGridControl1_ParseFindPanelText(object sender, DevExpress.Data.ParseFindPanelTextEventArgs e) {
    if (string.IsNullOrWhiteSpace(e.FindPanelText)) return;
    List<string> criteria = new List<string>();
    foreach (string criteriaString in e.FindPanelText.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) {
        string str = criteriaString;
        if (criteriaString[0] == ' ')
            str = criteriaString.Remove(0, 1);
        criteria.Add(str);
    }

    if (criteria.Count < 2)
        return;

    string filterString = String.Empty;
    for (int i = 0; i < criteria.Count; i++) {
        filterString += "Contains([Caption], '" + criteria[i] + "')";
        if (i != criteria.Count - 1)
            filterString += " OR ";
    }

    CriteriaOperator _findCriteria = CriteriaOperator.Parse(filterString);
    e.SetFindCriteriaAndHighlightedRangesGetterFromDisplayTextAndFieldName(_findCriteria, 
        (x, y) => {
        var arr = new DisplayTextHighlightRange[criteria.Count()];
        for (int i = 0; i < criteria.Count(); i++) {
            arr[i] = new DisplayTextHighlightRange(x.IndexOf(criteria[i]), criteria[i].Length);
        }
        return arr;
    });
    e.Handled = true;
}
vb
Imports DevExpress.Data
Imports DevExpress.Data.Filtering

Private Sub PropertyGridControl1_ParseFindPanelText(ByVal sender As Object, ByVal e As DevExpress.Data.ParseFindPanelTextEventArgs) _
    Handles propertyGridControl1.ParseFindPanelText

    If String.IsNullOrWhiteSpace(e.FindPanelText) Then
        Return
    End If
    Dim criteria As New List(Of String)()
    For Each criteriaString As String In e.FindPanelText.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
        Dim str As String = criteriaString
        If criteriaString.Chars(0) = " "c Then
            str = criteriaString.Remove(0, 1)
        End If
        criteria.Add(str)
    Next criteriaString

    If criteria.Count < 2 Then
        Return
    End If

    Dim filterString As String = String.Empty
    For i As Integer = 0 To criteria.Count - 1
        filterString &= "Contains([Caption], '" & criteria(i) & "')"
        If i <> criteria.Count - 1 Then
            filterString &= " OR "
        End If
    Next i

    Dim _findCriteria As CriteriaOperator = CriteriaOperator.Parse(filterString)
    e.SetFindCriteriaAndHighlightedRangesGetterFromDisplayTextAndFieldName(_findCriteria,
        Function(x, y)
            Dim arr = New DisplayTextHighlightRange(criteria.Count() - 1) {}
            For i As Integer = 0 To criteria.Count() - 1
                arr(i) = New DisplayTextHighlightRange(x.IndexOf(criteria(i)), criteria(i).Length)
            Next i
            Return arr
        End Function)
    e.Handled = True
End Sub

The code below shows how to search for an exact match with the query in a particular column.

csharp
using DevExpress.Data;
using DevExpress.Data.Filtering;

private void gridView1_ParseFindPanelText(object sender, DevExpress.Data.ParseFindPanelTextEventArgs e) {
    string searchField = "Country";
    CriteriaOperator criterion = new BinaryOperator(searchField, e.FindPanelText, BinaryOperatorType.Equal);
    e.Handled = true;
    e.SetFindCriteriaAndHighlightedRangesGetterFromDisplayTextAndFieldName(criterion, (displayText, fieldName) => {
        if (fieldName != searchField)
            return null;
        if (string.IsNullOrEmpty(displayText))
            return null;
        var indexOf = displayText.IndexOf(e.FindPanelText, StringComparison.CurrentCultureIgnoreCase);
        if (indexOf < 0)
            return null;
        return new DisplayTextHighlightRange(indexOf, e.FindPanelText.Length);
    });
}
vb
Imports DevExpress.Data
Imports DevExpress.Data.Filtering

Private Sub gridView1_ParseFindPanelText(ByVal sender As Object, ByVal e As DevExpress.Data.ParseFindPanelTextEventArgs) _
    Handles gridView1.ParseFindPanelText
    Dim searchField As String = "Country"
    Dim criterion As CriteriaOperator = New BinaryOperator(searchField, e.FindPanelText, BinaryOperatorType.Equal)
    e.Handled = True
    e.SetFindCriteriaAndHighlightedRangesGetterFromDisplayTextAndFieldName(criterion, Function(displayText, fieldName)
        If fieldName <> searchField Then
            Return Nothing
        End If
        If String.IsNullOrEmpty(displayText) Then
            Return Nothing
        End If
        Dim indexOf = displayText.IndexOf(e.FindPanelText, StringComparison.CurrentCultureIgnoreCase)
        If indexOf < 0 Then
            Return Nothing
        End If
        Return New DisplayTextHighlightRange(indexOf, e.FindPanelText.Length)
    End Function)
End Sub

For performance reasons, only the first occurrence of the search query is highlighted in a column.

The code below shows how to highlight all occurrences.

csharp
using DevExpress.Data;
using DevExpress.Data.Filtering;
using DevExpress.XtraGrid.Views.Grid;
using System;

private void gridView1_ParseFindPanelText(object sender, DevExpress.Data.ParseFindPanelTextEventArgs e) {
    GridView view = sender as GridView;
    CriteriaOperator _findCriteria = CriteriaOperator.Parse(string.Format("Contains([CompanyName],'{0}')", e.FindPanelText));
    e.SetFindCriteriaAndHighlightedRangesGetterFromDisplayTextAndFieldName(_findCriteria, (displayText, fieldName) => {
        if (fieldName != "CompanyName")
            return null;
        if (string.IsNullOrEmpty(displayText))
            return null;
        var indexOf = 0;
        int length = e.FindPanelText.Length;
        List<DisplayTextHighlightRange> res = new List<DisplayTextHighlightRange>();
        do {
            indexOf = displayText.IndexOf(e.FindPanelText, indexOf, StringComparison.CurrentCultureIgnoreCase);
            if (indexOf >= 0) {
                res.Add(new DisplayTextHighlightRange(indexOf, length));
                indexOf += length;
            }
        } while (indexOf >= 0);
        return res.ToArray();
    });
}
vb
Imports DevExpress.Data
Imports DevExpress.Data.Filtering
Imports DevExpress.XtraGrid.Views.Grid
Imports System

Private Sub gridView1_ParseFindPanelText(ByVal sender As Object, ByVal e As DevExpress.Data.ParseFindPanelTextEventArgs) Handles gridView1.ParseFindPanelText
    Dim view As GridView = TryCast(sender, GridView)
    Dim _findCriteria As CriteriaOperator = CriteriaOperator.Parse(String.Format("Contains([CompanyName],'{0}')", e.FindPanelText))
    e.SetFindCriteriaAndHighlightedRangesGetterFromDisplayTextAndFieldName(_findCriteria, Function(displayText, fieldName)
        If fieldName <> "CompanyName" Then
            Return Nothing
        End If
        If String.IsNullOrEmpty(displayText) Then
            Return Nothing
        End If
        Dim indexOf = 0
        Dim length As Integer = e.FindPanelText.Length
        Dim res As New List(Of DisplayTextHighlightRange)()
        Do
            indexOf = displayText.IndexOf(e.FindPanelText, indexOf, StringComparison.CurrentCultureIgnoreCase)
            If indexOf >= 0 Then
                res.Add(New DisplayTextHighlightRange(indexOf, length))
                indexOf += length
            End If
        Loop While indexOf >= 0
        Return res.ToArray()
    End Function)
End Sub

See Also

Find Panel Syntax

Grid Find Panel

Grid Find Panel Tutorial

ColumnView Class

ColumnView Members

DevExpress.XtraGrid.Views.Base Namespace