SandGrid User Guide

Editing

Back to Table of Contents

SandGrid supports editing of cell values, whether or not you are using cells. We ship some common editors with the product and you can easily create your own to fully customize the control.

Built in Editors

Most of the time you will only require simple textual editing of values in your columns. The SandGridTextBoxEditor class serves this purpose. The data type of the column being edited does not matter as the formatting and parsing process will usually take care of converting to text and back again for the editing process. The textbox editor simply shown a textbox as its UI.

SandGridComboBoxEditor is useful where you need a list of standard values for a cell. It will therefore usually need to be customized by the host application as it is being displayed, as only the host application knows the standard values for a cell. This type of editor is used by the GridForeignKeyColumn, which automatically populates the dropdown with values from the primary key table.

SandGridDateTimeEditor is the standard editor for DateTime columns, and simply wraps the standard Windows Forms DateTimePicker control. It can be configured through the column or by the host application to show the date in a number of different formats.

The value of a cell being edited can be set to NULL by pressing control-zero while the focus is within the editor.

Controlling Editing

By default editing is disabled in the grid. To enable editing you need to set the MouseEditMode and KeyboardEditMode properties of the grid. Editing with the mouse can be triggered with either a double click (like a datagrid) or after a delayed single click (like a ListView or a TreeView). Editing with the keyboard can be invoked by typing (like a spreadsheet) or by pressing F2.

As a cell is about to enter edit mode the grid raises the BeforeEdit event. The grid has already created the editor by this point, so your application can set scenario-specific properties on the editor at this point if it needs to.

Custom Editors

The editor that is created for a particular column depends on the EditorType property of the column. An editor class needs to derive from System.Windows.Forms.Control and additionally implement the IGridCellEditor interface. This is a simple interface that SandGrid uses to communicate with the editor, for example getting and setting its value and initializing it.

The important property on IGridCellEditor is the EditorValue property. SandGrid assigns to this when it needs to update the value in the editor, and you need to ensure that your control copes with null values correctly here. In the property getter you should return null if you have previously been assigned it, otherwise the value in the editor. An example of an implementation of this property follows.

public object EditorValue
{
        get
        {
                if (isNull)
                        return null;
                else
                        return Text;
        }
        set
        {
                string text = value as string;
                if (text == null)
                {
                        Text = "";
                        isNull = true;
                }
                else
                {
                        Text = text;
                        isNull = false;
                }
        }
}

You should also ensure that in your custom editor, you set SandGrid.EditorDirty to true when the user has made a change, otherwise your changed value will not be persisted. You can store the SandGrid reference from the InitializeContext method.

Next: Controlling Data