The grid provides several methods of data editing, including dropdown editors. You should know this feature from PropertyGrid component that is a part of the standard library. Editors in .Net Grid are also based on UITypeEditor class that can display any controls in a dropdown holder. An example of its use is provided below.

C# Copy imageCopy
//Example of a drop-down editor where the end-user can click on the push button
public class CustomEditor : UITypeEditor
{
    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        // Uses the IWindowsFormsEditorService to display a drop-down UI in the grid. 
        // See System.Drawing.Design.UITypeEditor for more details
        IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

        //The result to be returned
        object result = value;

        //Create some UI control
        Button button = new Button();
        button.Click += delegate
        {
            //result = ... <new value>
            service.CloseDropDown();
        };
        //Do edit in place in the editor service. If the end user clicks on the button, then the callback will be called.
        service.DropDownControl(button);

        return result;
    }

    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        //The style is drop-down list box
        return UITypeEditorEditStyle.DropDown;
    }
}


//Some data object
public class Car 
{
    //Some fields
    private double price;
    private double maxSpeed;
    private Color color;

    //Standard declaration of the custom editor.
    //See System.ComponentModel.EditorAttribute for more details
    [Editor(typeof(CustomEditor), typeof(UITypeEditor))]
    public double MaxSpeed
    {
        get { return maxSpeed; }
        set { maxSpeed = value; }
    }

    //The declaration of the Editor can be done in the column
    public double Price
    {
        get { return price; }
        set { price = value; }
    }

    //Default color editor is used (System.Drawing.Design.ColorEditor). 
    //This editor is returned by the TypeDescriptor.GetEditor(typeof (Color), typeof (UITypeEditor)) call
    public Color Color
    {
        get { return color; }
        set { color = value; }
    }
}


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

    grid.Headers[0]["MaxSpeed"].Editable = true;
    grid.Headers[0]["Price"].Editable = true;
    //Declare the editor only for the current grid
    grid.Headers[0]["Price"].Editor = new CustomEditor();
    grid.Headers[0]["Color"].Editable = true;

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

    //Edit the cell programmatically
    grid.Rows[0]["Price"].Edit();

    //...
}

Back to .Net Grid HowTo topics