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

Indicate the Editor Type in RadGridView Columns

Problem 

You want to indicate the column type when the grid is first shown. For example, if you have GridViewComboBoxColumn you will want the user to know that he or she can choose from a list of predefined items.

indicate-the-editor-type-in-radgridview-columns 001

Solution 

Create a custom column which contains an element that indicates the column type. For example, if you have a GridViewComboBoxColumn, you can add an arrow button that looks-like the one in the default drop down editor.

First, you can create a class that inherits the GridComboBoxCellElement. Afterwards, you can add the indicator element (button element in this example) and style it to look like the default editor. You can subscribe to the Click event of the button and directly show the editor when the button is clicked as well.

public class IndicatedComboBoxCellElement : GridComboBoxCellElement
{
    private RadDropDownListArrowButtonElement indicator;

    public IndicatedComboBoxCellElement(GridViewColumn column, GridRowElement row) : base(column, row)
    {
        //this code adds a registration for RadDropDownListArrowButtonElement in order to allow its usage in other controls
        Theme theme = ThemeRepository.ControlDefault;
        StyleGroup sg = theme.FindStyleGroup("Telerik.WinControls.UI.RadDropDownList");
        sg.Registrations.Add(new StyleRegistration("Telerik.WinControls.UI.RadDropDownListArrowButtonElement"));

        indicator = new RadDropDownListArrowButtonElement();
        indicator.MaxSize = new System.Drawing.Size(18, 20);
        indicator.Alignment = ContentAlignment.MiddleRight;
        indicator.NotifyParentOnMouseInput = false;
        indicator.Click += indicator_Click;

        this.Children.Add(indicator);
    }

    protected override void OnCellFormatting(CellFormattingEventArgs e)
    {
        base.OnCellFormatting(e);
        if (indicator != null)
        {
            indicator.Visibility = ((IndicatedComboBoxColumn)this.ColumnInfo).EnableIndicator == true ? ElementVisibility.Visible : ElementVisibility.Collapsed;
        }
    }

    void indicator_Click(object sender, EventArgs e)
    {
        this.GridControl.CellEditorInitialized += grid_CellEditorInitialized;

        this.GridControl.EndEdit();
        this.GridControl.BeginEdit();
    }

    void grid_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
        if (editor != null)
        {
            this.GridControl.CellEditorInitialized -= grid_CellEditorInitialized;

            RadDropDownListEditorElement element = editor.EditorElement as RadDropDownListEditorElement;
            element.ShowPopup();
        }
    }

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

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

Now, you are ready to create the custom column class. In it you can create a property, which will show or hide the indicator element. Despite that the column just inherits the default GridViewComboBoxColumn.

public class IndicatedComboBoxColumn : GridViewComboBoxColumn
{
    private bool enableIndicator;

    public IndicatedComboBoxColumn()
    {
        enableIndicator = true;
    }

    public bool EnableIndicator
    {
        get
        {
            return this.enableIndicator;
        }
        set
        {
            this.enableIndicator = value;
            this.OwnerTemplate.Refresh();
        }
    }

    public override Type GetCellType(GridViewRowInfo row)
    {
        if (row is GridViewDataRowInfo)
        {
            return typeof(IndicatedComboBoxCellElement);
        }
        return base.GetCellType(row);
    }
}

Nevertheless, keep in mind that this approach can be easily adopted for other column types by changing the inherited column and the styles of the indicator (other element can be used as well). In the sample projects here you can find a sample implementation for all columns that can have visible editors.

In this article