Set default filter operators in RadGridView

Environment

Product Version Product Author
2018.3.1016 RadGridView for WinForms Desislava Yordanova

Description

The default filter operator in the RadGridView's columns depends on the data type that is being stored in that column. For example, the default filter operator for a text column is Contains. This article will demonstrate a sample approach how to change it and apply another relevant filter operator for this data type.

set-default-filter-operators-in-gridview 001

Solution

Subscribe to the RadGridView.CreateCell event and replace the default GridFilterCellElement with your custom one where you can specify the default filter operator, e.g. StartsWith:

Custom filter cell element


        private void radGridView1_CreateCell(object sender, Telerik.WinControls.UI.GridViewCreateCellEventArgs e)
        {
            if (e.CellType == typeof(GridFilterCellElement) && e.Column.Name == "ProductName")
            {
                e.CellElement = new CustomGridFilterCellElement(e.Column as GridViewDataColumn,e.Row);
            }
        }

        public class CustomGridFilterCellElement : GridFilterCellElement
        {
            protected override Type ThemeEffectiveType    
            {
                get   
                {
                    return typeof(GridFilterCellElement);    
                }
            }

            public CustomGridFilterCellElement(GridViewDataColumn column, GridRowElement row) : base(column, row)
            {
                this.FilteringRowInfo.SuspendPropertyNotifications();
                this.SetFilterOperator(Telerik.WinControls.Data.FilterOperator.StartsWith);
                this.FilteringRowInfo.ResumePropertyNotifications();
            }
        }      

    Private Sub radGridView1_CreateCell(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCreateCellEventArgs)
        If e.CellType = GetType(GridFilterCellElement) AndAlso e.Column.Name = "ProductName" Then
            e.CellElement = New CustomGridFilterCellElement(TryCast(e.Column, GridViewDataColumn), e.Row)
        End If
    End Sub

    Public Class CustomGridFilterCellElement
    Inherits GridFilterCellElement

        Protected Overrides ReadOnly Property ThemeEffectiveType As Type
            Get
                Return GetType(GridFilterCellElement)
            End Get
        End Property

        Public Sub New(ByVal column As GridViewDataColumn, ByVal row As GridRowElement)
            MyBase.New(column, row)
            Me.FilteringRowInfo.SuspendPropertyNotifications()
            Me.SetFilterOperator(Telerik.WinControls.Data.FilterOperator.StartsWith)
            Me.FilteringRowInfo.ResumePropertyNotifications()
        End Sub
    End Class


As a result, the default filter operator for the ProductName column is changed:

set-default-filter-operators-in-gridview 002

See Also

In this article
Not finding the help you need? Improve this article