Edit this page

Using custom editors

This following snippet demonstrates how to replace the standard spin editor with a track bar.

All grid editors inherit from BaseGridEditor. So, you have to inherit from this class and override several methods:

public class TrackBarEditor : BaseGridEditor
{
    public override object Value
    {
        get
        {
            TrackBarEditorElement editor = (TrackBarEditorElement)this.EditorElement;
            return editor.Value;
        }
        set
        {
            TrackBarEditorElement editor = (TrackBarEditorElement)this.EditorElement;
            if (value != null && value != DBNull.Value)
            {
                editor.Value = Convert.ToInt32(value);
            }
            else
            {
                editor.Value = 0;
            }
        }
    }
    public override void BeginEdit()
    {
        base.BeginEdit();
        this.EditorElement.Focus();
        ((TrackBarEditorElement)this.EditorElement).TrackPositionChanged += new EventHandler(TrackBarEditor_TrackPositionChanged);
    }
    public override bool EndEdit()
    {
        ((TrackBarEditorElement)this.EditorElement).TrackPositionChanged -= new EventHandler(TrackBarEditor_TrackPositionChanged);
        return base.EndEdit();
    }
    void TrackBarEditor_TrackPositionChanged(object sender, EventArgs e)
    {
        OnValueChanged();
    }
    protected override RadElement CreateEditorElement()
    {
        return new TrackBarEditorElement();
    }
}

Public Class TrackBarEditor
    Inherits BaseGridEditor
    Public Overrides Property Value() As Object
        Get
            Dim editor As TrackBarEditorElement = CType(Me.EditorElement, TrackBarEditorElement)
            Return editor.Value
        End Get
        Set(ByVal value As Object)
            Dim editor As TrackBarEditorElement = CType(Me.EditorElement, TrackBarEditorElement)
            If value IsNot Nothing AndAlso value IsNot DBNull.Value Then
                editor.Value = Convert.ToInt32(value)
            Else
                editor.Value = 0
            End If
        End Set
    End Property
    Public Overrides Sub BeginEdit()
        MyBase.BeginEdit()
        Me.EditorElement.Focus()
        AddHandler (CType(EditorElement, TrackBarEditorElement)).TrackPositionChanged, AddressOf TrackBarEditor_TrackPositionChanged
    End Sub
    Public Overrides Function EndEdit() As Boolean
        RemoveHandler (CType(EditorElement, TrackBarEditorElement)).TrackPositionChanged, AddressOf TrackBarEditor_TrackPositionChanged
        Return MyBase.EndEdit()
    End Function
    Private Sub TrackBarEditor_TrackPositionChanged(ByVal sender As Object, ByVal e As EventArgs)
        OnValueChanged()
    End Sub
    Protected Overrides Function CreateEditorElement() As RadElement
        Return New TrackBarEditorElement()
    End Function
End Class

We use the standard RadTrackBar element in this example with some modification to enable keyboard navigation:

public class TrackBarEditorElement : RadTrackBarElement
{
    public TrackBarEditorElement()
    {
        this.CanFocus = true;
    }
    public event EventHandler TrackPositionChanged;
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(RadTrackBarElement);
        }
    }
    protected override SizeF MeasureOverride(SizeF availableSize)
    {
        int desiredHeight = 30;
        foreach (RadElement element in this.Children)
        {
            element.Measure(new SizeF(availableSize.Width, desiredHeight));
        }
        return new SizeF(1, desiredHeight);
    }
    protected override void OnPropertyChanged(RadPropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);
        if (e.Property == RadTrackBarItem.ValueProperty
            && this.Parent != null
            && this.TrackPositionChanged != null)
        {
            this.TrackPositionChanged(this, EventArgs.Empty);
        }
    }
}

Public Class TrackBarEditorElement
    Inherits RadTrackBarElement
    Public Sub New()
        Me.CanFocus = True
    End Sub
    Public Event TrackPositionChanged As EventHandler
    Protected Overrides ReadOnly Property ThemeEffectiveType() As Type
        Get
            Return GetType(RadTrackBarElement)
        End Get
    End Property
    Protected Overrides Function MeasureOverride(ByVal availableSize As System.Drawing.SizeF) As System.Drawing.SizeF
        Dim desiredHeight As Integer = 30
        For Each element As RadElement In Me.Children
            element.Measure(New System.Drawing.SizeF(availableSize.Width, desiredHeight))
        Next element
        Return New System.Drawing.SizeF(1, desiredHeight)
    End Function
    Protected Overrides Sub OnPropertyChanged(ByVal e As RadPropertyChangedEventArgs)
        MyBase.OnPropertyChanged(e)
        If e.Property Is RadTrackBarItem.ValueProperty AndAlso Me.Parent IsNot Nothing AndAlso Me.TrackPositionChangedEvent IsNot Nothing Then
            RaiseEvent TrackPositionChanged(Me, EventArgs.Empty)
        End If
    End Sub
End Class

The EditorRequired event is the correct place to replace the default editor:

void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
{
    if (e.EditorType == typeof(GridSpinEditor))
    {
        e.EditorType = typeof(TrackBarEditor);
    }
}

Private Sub RadGridView1_EditorRequired(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.EditorRequiredEventArgs) Handles RadGridView1.EditorRequired
    If e.EditorType Is GetType(GridSpinEditor) Then
        e.EditorType = GetType(TrackBarEditor)
    End If
End Sub

note

You can find a working version of this sample in our Demo application ( GridView -> Custom Editors )

See Also

Was this article helpful? Yes No
Thank you for your feedback!

Give article feedback

Tell us how we can improve this article

close
Dummy