Articles in this section
Category / Section

How to change the default compare operator in WinForms GridGroupingControl?

4 mins read

Customize compare operator

The WinForms 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

 

Conclusion

I hope you enjoyed learning about how to change the default compare operator in WinForms GridGroupingControl.

You can refer to our WinForms GridGrouping Control’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridGrouping Control documentation to understand how to present and manipulate data.

For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms GridGrouping Control and other WinForms components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied