You can use UITypeEditorEx class that expands UITypeEditor functionality to create editors over cells and their dimensions. Please see below an example of a trackbar editor that draws a trackbar when no editing is performed and creates a trackbar control for the cell when a user clicks it.

C# Copy imageCopy
// TrackBar editor to edit numeric values. 
public class TrackBarEditor : UITypeEditorEx
{
    private readonly int minValue;
    private readonly int maxValue;

    public TrackBarEditor(int minValue, int maxValue)
    {
        if (minValue >= maxValue)
        {
            throw new ArgumentException("The minValue must be less than maxValue");
        }

        this.minValue = minValue;
        this.maxValue = maxValue;
    }

    public int MinValue
    {
        get { return minValue; }
    }

    public int MaxValue
    {
        get { return maxValue; }
    }

    public override bool GetPaintCellSupported()
    {
        //The cell should be repainted in the editor
        return true;
    }

    public override void PaintCell(PaintCellEventArgs e)
    {
        //Do basic painting without text
        e.Text = string.Empty;
        e.PaintAll();
        e.Handled = true;


        Rectangle r = e.Cell.VisibleBounds;
        r.Inflate(-4, 0);
        int value = (int) e.Cell.Value;

        int range = maxValue - minValue;
        if(range > 0)
        {
            Rectangle channel = new Rectangle(r.X, r.Y + r.Height / 2 - 3, r.Width, 4);
            Rectangle button = new Rectangle(r.X + (value * (r.Width - 4) / range), r.Y + 3, 5, r.Height - 6);

            //Draw the track channel
            ControlPaint.DrawBorder3D(e.Graphics, channel, Border3DStyle.SunkenInner);

            //Draw the thumb
            if (Application.RenderWithVisualStyles)
            {
                VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.TrackBar.ThumbBottom.Hot);
                renderer.DrawBackground(e.Graphics, button);
            }
            else
            {
                ControlPaint.DrawButton(e.Graphics, button, ButtonState.Normal);
            }
        }
    }

    public override void EditCell(IGridEditorService service, Cell cell, StartEditReason reason)
    {
        //Edit the cell if the user has clicked on the cell with the left button
        if (cell.Row != null && Equals(StartEditReason.LButtonClick, reason))
        {
            cell.EnsureVisible();
            //Create a real control
            using (TrackBar control = new TrackBar())
            {
                //Initialize the control
                control.BackColor = cell.Appearance.BackColor;
                control.TickStyle = TickStyle.None;
                control.Minimum = minValue;
                control.Maximum = maxValue;
                int value = Convert.ToInt32(cell.Value);
                value = Math.Max(control.Minimum, value);
                value = Math.Min(control.Maximum, value);
                control.Value = value; 

                //Handle the MouseUp events
                control.MouseUp += delegate
                {
                    //Stop edit in place
                    service.CloseCellControl(StopEditReason.Undefined);
                };

                //Start Edit in place. 
                service.CellEditControl(control, cell.VirtualBounds, reason, true);

                //Set a new value to Cell
                cell.Value = Convert.ChangeType(control.Value, cell.DataField.FieldType); 
            }
        }
    }
}


//Some data object
public class Product
{
    //Some fields
    private int quantity;

    //Declaration of the TrackBarEditor. 
    [Editor(typeof(TrackBarEditor), typeof(UITypeEditor))]
    public int Quantity
    {
        get { return quantity; }
        set { quantity = value; }
    }
}


//Initialize the grid
public void AddDataObjectToGrid(Grid grid)
{
    //Configure the headers
    grid.Headers.Add(new Header());
    grid.Headers[0].Add(new Column("Quantity"));
    grid.Headers[0]["Quantity"].Editable = true;

    //Add a data object
    grid.Rows.Add(new Product());
}

Back to .Net Grid HowTo topics