)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to change the default compare operator in WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: May 13, 2015 |
Last Revised Date: December 23, 2019
Tags: filtering

Customize compare operator

The GridGroupingControl contains different types of compare operators for DynamicFilter like, "StartsWith", "EndsWith", "Equals", "NotEquals", "LessThan", "LessThanOrEqualTo", "GreaterThan", "GreaterThanOrEqualTo", "Like". The default compare operator is “StartsWith”. To change these, compare operators, use the following customization.

Solution

You can change the compare operators by customizing the FilterBarCell model and renderer. GridTableFilterBarExtCellModel/GridTableFilterBarExtCellRenderer class that is derived from GridComboBoxCellModel/GridComboBoxCellRenderer class are customized for modifying the compare operator’s ListBox. The images of compare operators are drawn by using PaintIcon method.

GridGroupingExtBitmaps class is used to retrieve the exact Bitmap from the application path.

C#

ListBox compareOprListBox = null;
this.compareOprListBox = (ListBox)new GridComboBoxListBoxPart();
//Add the compare operators in ListBox
this.compareOprListBox.Items.AddRange(new string[] { "Expression Match","StartsWith", "EndsWith" });
GridTableFilterBarExtCellRenderer renderer = (GridTableFilterBarExtCellRenderer)Owner;
//Get the bitmap images for compare operators
Bitmap bm = renderer.Model.GetCompareOperatorImage(style);
GridGroupingExtBitmaps.IconPainter.PaintIcon(g, Bounds, ptOffset, bm, Color.Black);
//return the bitmap images for compare operator
internal Bitmap GetCompareOperatorImage(GridStyleInfo style)
{
    GridTableCellStyleInfo tableCell = (GridTableCellStyleInfo)style;
    string name = GetCompareOperatorName(tableCell.TableCellIdentity.Column.Name);
    if (compareOprBitmaps == null)
        InitCompareOprBitmaps();
    if (Renderer.GetFilterBarText(style) == selectCustomText)
    {
        return GridGroupingExtBitmaps.GetBitmap("startswith");
    }
    if (name == string.Empty)
    {
        return GridGroupingExtBitmaps.GetBitmap("em");
    }
    if (compareOprBitmaps.ContainsKey(name))
    {
        if (name == "StartsWith")
            return GridGroupingExtBitmaps.GetBitmap("startswith");
        else if (name == "EndsWith")
            return GridGroupingExtBitmaps.GetBitmap("endswith");
        else if (name == "Expression Match")
            return GridGroupingExtBitmaps.GetBitmap("em");
    }
    if (customFilters != null && customFilters.Contains(name))
    {
        Bitmap bm = customFilters.GetImage(name);
        if (bm != null)
            return bm;
    }
    //Default Filter Bitmap
    return GridGroupingExtBitmaps.GetBitmap("default");
}

VB

Private compareOprListBox As ListBox = Nothing
Me.compareOprListBox = CType(New GridComboBoxListBoxPart(), ListBox)
‘Add the compare operators in ListBox
Me.compareOprListBox.Items.AddRange(New String() { "Expression Match","StartsWith", "EndsWith" })
Dim renderer As GridTableFilterBarExtCellRenderer = CType(Owner, GridTableFilterBarExtCellRenderer)
‘Get the bitmap images for compare operators
Dim bm As Bitmap = renderer.Model.GetCompareOperatorImage(style)
GridGroupingExtBitmaps.IconPainter.PaintIcon(g, Bounds, ptOffset, bm, Color.Black)
‘Return the bitmap images for compare operator
Friend Function GetCompareOperatorImage(ByVal style As GridStyleInfo) As Bitmap
   Dim tableCell As GridTableCellStyleInfo = CType(style, GridTableCellStyleInfo)
    Dim name As String = GetCompareOperatorName(tableCell.TableCellIdentity.Column.Name)
    If compareOprBitmaps Is Nothing Then
        InitCompareOprBitmaps()
    End If
    If Renderer.GetFilterBarText(style) = selectCustomText_Renamed Then
    Return GridGroupingExtBitmaps.GetBitmap("startswith")
    End If
    If name = String.Empty Then
        Return GridGroupingExtBitmaps.GetBitmap("em")
    End If
    If compareOprBitmaps.ContainsKey(name) Then
        If name = "StartsWith" Then
            Return GridGroupingExtBitmaps.GetBitmap("startswith")
        ElseIf name = "EndsWith" Then
            Return GridGroupingExtBitmaps.GetBitmap("endswith")
        ElseIf name = "Expression Match" Then
            Return GridGroupingExtBitmaps.GetBitmap("match")
        End If
    End If
    If customFilters IsNot Nothing AndAlso customFilters.Contains(name) Then
        Dim bm As Bitmap = customFilters.GetImage(name)
        If bm IsNot Nothing Then
            Return bm
        End If
    End If
    'Default Filter Bitmap
     Return GridGroupingExtBitmaps.GetBitmap("default")
End Function

The following code example is used to return the compare operator name for applying the filter option.

C#

//return the selected compare operator name 
protected object GetLogicalCompareOperator(string key)
{
    string name = GetCompareOperatorName(key);
    switch (name)
    {
        case "Like":
        case "StartsWith":
        case "EndsWith":
            return FilterCompareOperator.Like;
        case "Expression Match":
            return FilterCompareOperator.Match;
        default:
            return FilterCompareOperator.Custom;
    }
}

VB

‘return the selected compare operator name
Protected Function GetLogicalCompareOperator(ByVal key As String) As Object
   Dim name As String = GetCompareOperatorName(key)
   Select Case name
      Case "Like", "StartsWith", "EndsWith"
          Return FilterCompareOperator.Like
      Case "Match"
          Return FilterCompareOperator.Match
      Case Else
          Return FilterCompareOperator.Custom
   End Select
End Function

The following code example illustrates to apply the filter format based on the compare operator name.

C#

//Filter the value based on typing value
internal string IncludeFilterFormat(object value, object key)
{
    string oprName = GetCompareOperatorName(key);
    string filter = Convert.ToString(value);
    switch (oprName)
    {
        case "StartsWith":
            filter = string.Format("{0}*", value);
            break;
        case "EndsWith":
            filter = string.Format("*{0}", value);
            break;
        case "Custom":
            filter = string.Format("{0}", value);
            break;
        default:
            if (customFilters != null && customFilters.Contains(oprName))
            {
                string format = customFilters.GetExpression(oprName);
                format = format.Replace("{VALUE}", "{0}");
                filter = string.Format(format, value);
                filter = string.Format("[{0}] {1}", key, filter);
            }
            break;
    }
    return filter;
}

VB

‘Filter the value based on typing value
Friend Function IncludeFilterFormat(ByVal value As Object, ByVal key As Object) As String
   Dim oprName As String = GetCompareOperatorName(key)
   Dim filter As String = Convert.ToString(value)
   Select Case oprName
     Case "StartsWith"
       filter = String.Format("{0}*", value)
     Case "EndsWith"
       filter = String.Format("*{0}", value)
     Case "Custom"
       filter = String.Format("{0}", value)
     Case Else
       If customFilters IsNot Nothing AndAlso customFilters.Contains(oprName) Then
            Dim format As String = customFilters.GetExpression(oprName)
                format = format.Replace("{VALUE}", "{0}")
                filter = String.Format(format, value)
                filter = String.Format("[{0}] {1}", key, filter)
       End If
   End Select
   Return filter
End Function

C#

//Filter the value based on typed value
internal virtual string ExcludeFilterFormat(object value, object key)
{
    string oprName = GetCompareOperatorName(key);
    string filter = Convert.ToString(value);
    switch (oprName)
    {
        case "StartsWith":
            if (filter.EndsWith("*"))
                filter = filter.Remove(filter.Length - 1);
            break;
        case "EndsWith":
            if (filter.StartsWith("*"))
                filter = filter.Remove(0, 1);
            break;
        default:
            if (this.customFilters != null && this.customFilters.Contains(oprName))
            {
                string format = this.customFilters.GetExpression(oprName);
                format = format.Replace("{VALUE}", "{0}");
                filter = string.Format(format, value);
                filter = string.Format("[{0}] {1}", key, filter);
            }
            break;
    }
    return filter;
}

VB

‘Filter the value based on typing value
Friend Overridable Function ExcludeFilterFormat(ByVal value As Object, ByVal key As Object) As String
    Dim oprName As String = GetCompareOperatorName(key)
    Dim filter As String = Convert.ToString(value)
    Select Case oprName
      Case "StartsWith"
        If filter.EndsWith("*") Then
           filter = filter.Remove(filter.Length - 1)
        End If
      Case "EndsWith"
        If filter.StartsWith("*") Then
           filter = filter.Remove(0, 1)
        End If
        If Me.customFilters IsNot Nothing AndAlso Me.customFilters.Contains(oprName) Then
           Dim format As String = Me.customFilters.GetExpression(oprName)
           format = format.Replace("{VALUE}", "{0}")
           filter = String.Format(format, value)
           filter = String.Format("[{0}] {1}", key, filter)
        End If
    End Select
  Return filter
End Function

The following screenshot displays the customized compare operators.

Customize the compare operator in GridGroupingControl

Figure 1: Customized compare operators

Samples:

C#: CompareOperator_CS

VB: CompareOperator_VB

Reference link: https://help.syncfusion.com/windowsforms/classic/gridgroupingcontrol/filtering

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon