New to Telerik UI for WinForms? Download free 30-day trial

How to Filter GridViewCommandColumn in RadGridView

Environment

Product Version Product Author
2022.3.913 RadGridView for WinForms Desislava Yordanova

Description

By default, filtering is not supposed to be supported for the GridViewCommandColumn. This article demonstrates a sample approach how to achieve such functionality.

Solution

It is necessary to create a derivative of the GridViewCommandColumn. Then, overriding the AllowFiltering property for the custom GridViewCommandColumn allows you to enable filtering functionality. In order to show the filter operator ("Contains", "Starts With"), it is necessary to create a custom GridFilterCellElement and override its SetSelectedFilterOperatorText method which is skipped by default for the GridViewCommandColumn. A sample code snippet is demonstrated below:

filter-commandcolumn-in-gridview 001


private void RadForm1_Load(object sender, EventArgs e)
{ 
    this.productsTableAdapter.Fill(this.nwindDataSet.Products);

    this.radGridView1.EnableFiltering = true;

    FilterCommandColumn commandColumn = new FilterCommandColumn();
    commandColumn.AutoFilterDelay = 500;
    commandColumn.Name = "CommandColumn";
    commandColumn.DataType = typeof(string);
    commandColumn.UseDefaultText = false;
    commandColumn.FieldName = "ProductName";
    commandColumn.HeaderText = "CommandColumn";
    radGridView1.MasterTemplate.Columns.Add(commandColumn);

    this.radGridView1.BestFitColumns();
}

public class FilterCommandColumn : GridViewCommandColumn
{
    public FilterCommandColumn() : base()
    {
    }

    public FilterCommandColumn(string name) : base(name)
    {
    }

    public override Type GetCellType(GridViewRowInfo row)
    {
        if (row is GridViewFilteringRowInfo)
        {
            return typeof(CustomGridFilterCellElement);
        }
        return base.GetCellType(row);
    }
    public override bool AllowFiltering
    {
        get 
        { 
            return this.IsDataBound;
        } 
    }

    public override bool AllowSearching
    {
        get
        {
            return this.IsDataBound;
        }
    }

    public override IInputEditor GetDefaultEditor()
    {
        return new RadTextBoxEditor();
    }

    public override Type GetDefaultEditorType()
    {
        return typeof(RadTextBoxEditor);
    }
}

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

    public CustomGridFilterCellElement(GridViewDataColumn column, GridRowElement row)
        : base(column, row)
    { 
    }

    public override void Attach(GridViewColumn data, object context)
    {
        base.Attach(data, context);
    }
    public override void Detach()
    {
        base.Detach();
    }

    protected override void SetSelectedFilterOperatorText()
    { 
        string text = RadGridLocalizationProvider.CurrentProvider.GetLocalizedString(RadGridStringId.FilterOperatorNoFilter);

        if (this.Descriptor != null)
        {
            RadGridLocalizationProvider currentProvider = RadGridLocalizationProvider.CurrentProvider;
            if (this.Descriptor is CompositeFilterDescriptor)
            {
                text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorCustom);
            }
            else
            {
                switch (this.Descriptor.Operator)
                {
                    case FilterOperator.IsLike:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorIsLike);
                        break;
                    case FilterOperator.IsNotLike:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorNotIsLike);
                        break;
                    case FilterOperator.IsLessThan:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorLessThan);
                        break;
                    case FilterOperator.IsLessThanOrEqualTo:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorLessThanOrEqualTo);
                        break;
                    case FilterOperator.IsEqualTo:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorEqualTo);
                        break;
                    case FilterOperator.IsNotEqualTo:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorNotEqualTo);
                        break;
                    case FilterOperator.IsGreaterThanOrEqualTo:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorGreaterThanOrEqualTo);
                        break;
                    case FilterOperator.IsGreaterThan:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorGreaterThan);
                        break;
                    case FilterOperator.StartsWith:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorStartsWith);
                        break;
                    case FilterOperator.EndsWith:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorEndsWith);
                        break;
                    case FilterOperator.Contains:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorContains);
                        break;
                    case FilterOperator.NotContains:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorDoesNotContain);
                        break;
                    case FilterOperator.IsNull:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorIsNull);
                        break;
                    case FilterOperator.IsNotNull:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorNotIsNull);
                        break;
                    case FilterOperator.IsContainedIn:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorIsContainedIn);
                        break;
                    case FilterOperator.IsNotContainedIn:
                        text = currentProvider.GetLocalizedString(RadGridStringId.FilterOperatorNotIsContainedIn);
                        break;
                    default:
                        text = RadGridLocalizationProvider.CurrentProvider.GetLocalizedString(RadGridStringId.FilterOperatorNoFilter);
                        break;
                }
            }
        }

        this.FilterOperatorText.Text = text + ":";
    }

    public override bool IsCompatible(GridViewColumn data, object context)
    {
        return data is FilterCommandColumn;
    }
}