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

How to Indicate RadTextBoxEditor in Grid Filter Cells

Environment

Product Version Product Author
2022.1.222 RadGridView for WinForms Desislava Yordanova

Description

RadGridView uses an editing mechanism which means that when you need to edit a single cell, an appropriate editor is activated. Once the editor's value is confirmed (e.g. by pressing Enter), the value is committed to the respective cell. The same approach is followed for the filter cells. Only a single cell can be in edit mode at a time. That is why the textbox is not visible until the filter cell enters edit mode.

Normal state Edit mode
indicate-textboxeditor-in-grid-filter-cell 001 indicate-textboxeditor-in-grid-filter-cell 002

A common requirement is to indicate the editor in the filter cells even though the grid is not in edit mode. This article demonstrates a sample approach how to indicate the RadTextBoxEditor in the filter cells for the GridViewTextBoxColumn. However, this approach can be adopted for other column types as well.

Solution

We will use a RadGridView bound to the Northwind.Products table. Then, we will replace the "ProductName" column with a custom one.

1. Create a derivative of GridFilterCellElement and add a RadTextBoxControlElement to it which will be visible only when the filter cell is NOT in edit mode. Once the editor is activated, the RadTextBoxControlElement will be collapsed. Its purpose is to indicate the end users that there is a textbox editor for filtering.

2. Create a derivative of GridViewTextBoxColumn and replace the default GridFilterCellElement with the custom one in the GetCellType method.

The achieved result is illustrated below:

indicate-textboxeditor-in-grid-filter-cell 003


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

    this.radGridView1.Columns.Remove("ProductName");
    CustomTextBoxColumn productNameColumn = new CustomTextBoxColumn();
    productNameColumn.Name = productNameColumn.FieldName = productNameColumn.HeaderText = "ProductName";
    this.radGridView1.Columns.Insert(1, productNameColumn);
    this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

    this.radGridView1.EnableFiltering = true;
}

public class CustomTextBoxColumn : GridViewTextBoxColumn
{
    public override Type GetCellType(GridViewRowInfo row)
    {
        if (row is GridViewFilteringRowInfo)
        {
            return typeof(CustomFilterCellElement);
        }
        return base.GetCellType(row);
    }
}

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

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

    RadTextBoxControlElement textBoxElement = new RadTextBoxControlElement();

    protected override void CreateChildElements()
    {
        base.CreateChildElements();
        textBoxElement.ShouldHandleMouseInput = false;
        textBoxElement.NotifyParentOnMouseInput = true;
        this.Children.Add(textBoxElement);
    } 
    protected override void SetContentCore(object value)
    {
        base.SetContentCore(value);
        this.textBoxElement.Text = value + ""; 
    }

    public override void AddEditor(IInputEditor editor)
    {
        this.textBoxElement.Visibility = ElementVisibility.Collapsed;
        base.AddEditor(editor);
    }

    public override void RemoveEditor(IInputEditor editor)
    {
        base.RemoveEditor(editor);
        this.textBoxElement.Visibility = ElementVisibility.Visible;
        this.RowInfo.InvalidateRow();
    }

    protected override SizeF ArrangeOverride(SizeF finalSize)
    {
        SizeF s = base.ArrangeOverride(finalSize);

        this.textBoxElement.Arrange(new RectangleF(this.FilterOperatorText.DesiredSize.Width, 2,
            finalSize.Width - this.FilterOperatorText.DesiredSize.Width - this.FilterButton.DesiredSize.Width - 10, s.Height - 5));
        return s;
    }
}