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

Custom Values in GridViewComboBoxColumn

Environment

Product Version 2018.1 220
Product RadGridView for WinForms

Description

By default one cannot add values that are not contained in the popup when GridViewComboBoxColumn is used. This article demonstrates how you can add any value and still be able to chose from predefined options.

Solution

Firs you need to add a custom editor, this way you can override the Value property and return the text when no value is selected.

Use Custom editor

class CustomDropDownListEditor : RadDropDownListEditor
{
    public override object Value
    {
        get
        {
            object result = base.Value;
            if (result == null || string.IsNullOrEmpty(result.ToString()))
            {
                var editor = this.EditorElement as RadDropDownListElement;
                return editor.Text;
            }
            return result;
        }
        set
        {
            base.Value = value;
            var editor = this.EditorElement as RadDropDownListElement;
            if (editor.SelectedValue == null)
            {
                editor.TextBox.TextBoxItem.Text = value.ToString();
            }
        }
    }
    public override void BeginEdit()
    {
        base.BeginEdit();
        var editor = this.EditorElement as RadDropDownListElement;
        editor.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown;
    }
}

Friend Class CustomDropDownListEditor
    Inherits RadDropDownListEditor
    Public Overrides Property Value() As Object
        Get
            Dim result As Object = MyBase.Value
            If result Is Nothing OrElse String.IsNullOrEmpty(result.ToString()) Then
                Dim editor = TryCast(Me.EditorElement, RadDropDownListElement)
                Return editor.Text
            End If
            Return result
        End Get
        Set(ByVal value As Object)
            MyBase.Value = value
            Dim editor = TryCast(Me.EditorElement, RadDropDownListElement)
            If editor.SelectedValue Is Nothing Then
                editor.TextBox.TextBoxItem.Text = value.ToString()
            End If
        End Set
    End Property
    Public Overrides Sub BeginEdit()
        MyBase.BeginEdit()
        Dim editor = TryCast(Me.EditorElement, RadDropDownListElement)
        editor.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown
    End Sub
End Class

Once the editor is ready you can change it by using the EditorRequired event.

Change Default Editor

private void RadGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
{
    if (e.EditorType == typeof(RadDropDownListEditor))
    {
        e.EditorType = typeof(CustomDropDownListEditor);
    }
}

Private Sub RadGridView1_EditorRequired(ByVal sender As Object, ByVal e As EditorRequiredEventArgs)
    If e.EditorType Is GetType(RadDropDownListEditor) Then
        e.EditorType = GetType(CustomDropDownListEditor)
    End If
End Sub

The final step is to create a custom column. This is necessary because the text is formated by looking in the DataSource of the column.

Create Custom Column

class CustomGridViewComboBoxColumn : GridViewComboBoxColumn
{
    public override object GetLookupValue(object cellValue)
    {
        object result = base.GetLookupValue(cellValue);
        if (result == null)
        {
            return cellValue;
        }
        return result;
    }
}

Friend Class CustomGridViewComboBoxColumn
    Inherits GridViewComboBoxColumn
    Public Overrides Function GetLookupValue(ByVal cellValue As Object) As Object
        Dim result As Object = MyBase.GetLookupValue(cellValue)
        If result Is Nothing Then
            Return cellValue
        End If
        Return result
    End Function
End Class

That is all now you will be able to add any value in the GridViewComBoxColumn. A complete Visual Studio solution is available here.

In this article