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

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();
    }
}

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);
        }
    }
}

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);
    }
}

You can find a working version of this sample in our Demo application ( GridView -> Custom Editors ). To access the Live Demo simply click on the Windows Start button and type WinForms Demo. If you are not able to find the Live Demos using that approach you can also download it directly from here.