Editors based on standard UITypeEditor component are not sufficient to edit data directly above cells. They also have limitations related to data painting. To expand available abilities we have created UITypeEditorEx class that is an inheritor of UITypeEditor class and expands its functionality. You can use this class to edit cell data with controls on top of cells or without such controls. Unlike standard editors, such editors may paint at any part of the cell. Below you can see an example of an editor that edits the cell without creating a control.

C# Copy imageCopy
//Example of checkbox editor
public class CheckBoxEditor : UITypeEditorEx
{
    public override bool GetPaintCellSupported()
    {
        return true;
    }

    public override void PaintCell(PaintCellEventArgs e)
    {
        e.Parts &= e.Parts ^ PaintPart.Text;

        //Default drawing
        base.PaintCell(e);

        Rectangle bounds = e.Cell.VirtualBounds;
        int height = Math.Min(SystemInformation.HorizontalScrollBarHeight, bounds.Height);
        int width = Math.Min(SystemInformation.VerticalScrollBarWidth, bounds.Width);

        bounds.X += (bounds.Width - width) / 2;
        bounds.Width = width;
        bounds.Y += (bounds.Height - height) / 2;
        bounds.Height = height;


        if (Equals(true, e.Cell.Value))
        {
            ControlPaint.DrawCheckBox(e.Graphics, bounds, ButtonState.Checked | ButtonState.Flat);
        }
        else if (Equals(false, e.Cell.Value))
        {
            ControlPaint.DrawCheckBox(e.Graphics, bounds, ButtonState.Normal | ButtonState.Flat);
        }
        else
        {
            ControlPaint.DrawCheckBox(e.Graphics, bounds, ButtonState.Normal | ButtonState.Inactive | ButtonState.Flat);
        }
    }

    public override void EditCell(IGridEditorService service, Cell cell, StartEditReason reason)
    {
        object value = cell.Value;
        cell.Value = !Equals(true, value);
    }

    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.None;
    }
}


//Some data object
public class Product
{
    private bool active;
    private bool tradable;

    //Declare the CheckBoxEditor for this property. Each Grid will use this editor to edit values in cells
    [Editor(typeof(CheckBoxEditor), typeof(UITypeEditor))]
    public bool Active
    {
        get { return active; }
        set { active = value; }
    }

    //The CheckBoxEditor may be declared in the Column 
    public bool Tradable
    {
        get { return tradable; }
        set { tradable = value; }
    }
}



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

    grid.Headers[0]["Active"].Editable = true;
    grid.Headers[0]["Tradable"].Editable = true;
    //Declare the editor only for the current grid
    grid.Headers[0]["Tradable"].Editor = new CheckBoxEditor();

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

Back to .Net Grid HowTo topics