New to Telerik UI for WinFormsStart a free 30-day trial

Set default filter operators in RadGridView

Updated over 1 year ago

Environment

Product VersionProductAuthor
2018.3.1016RadGridView for WinFormsDesislava 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

C#

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

public class DefaultGridFilterCellElement : GridFilterCellElement
{
    public DefaultGridFilterCellElement(GridViewDataColumn column, GridRowElement row)
        : base(column, row)
    {
    }

    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(GridFilterCellElement);
        }
    }

    public override bool IsCompatible(GridViewColumn data, object context)
    {
        return data.Name != "ProductName";
    }
}

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();
    }

    public override bool IsCompatible(GridViewColumn data, object context)
    {
        return data.Name == "ProductName";
    }
}
    

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

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

See Also