FlexGrid for WinForms | ComponentOne
Editing Cells / Custom Editors
In This Topic
    Custom Editors
    In This Topic

    The built-in editors provide a lot of flexibility and power, but in some cases you may want to use external controls as specialized editors. For example, you may want to use the C1NumericInput control that provides a drop-down calculator for entering numbers, or an editor for selecting from multi-column lists, or a specialized control that you wrote to edit your business objects.

    Any control that derives from the base Control class can be used as a basic grid editor. Controls that implement the IC1EmbeddedEditor interface (defined in C1.Common.dll) can provide better integration with the grid and more advanced features. For details on the IC1EmbeddedEditor interface, see the Editor property.

    To use a control as a custom editor, all you have to do is associate an instance of the control with a grid column or a style using its Editor property. You can do this in the designer (using the Column Editor) or in code. After that, the control will be automatically used by the grid.

    To define custom editors at design time, add an instance of the editor control to the form, then select Designer from the C1FlexGrid Tasks menu to open the C1FlexGrid Column Editor. Select the columns that should use the custom editor and set their Editor properties to the name of the new editor control.

    For example, to use a NumericUpDown control as a grid editor, follow these steps:

    1. Add a C1FlexGrid control to the form.
    2. Add a NumericUpDown control to the form and set its BorderStyle property to None.
    3. Select Designer from the C1FlexGrid Tasks menu. For more information on accessing the C1FlexGrid Column Editor, see Accessing the C1FlexGrid Column Editor.
    4. In the C1FlexGrid Column Editor, select the first scrollable grid column and set its Editor property to NumericUpDown1.

    Run the project and edit some values in the first column. Notice how the grid positions and initializes the NumericUpDown control so you can edit cell values. When you are done editing a cell, click a different cell or press the TAB key to move to the next one. Notice how the new value is applied to the cell.

    You can also assign custom editors to the grid using code:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Create the custom editor.
        Dim editor as New NumericUpDown()
        editor.BorderStyle = BorderStyle.None
       ' Assign the custom editor to the grid.
        _flex.Cols(1).Editor = editor
    End Sub

    To write code in C#

    Copy Code
    private void Form1_Load(object sender, System.EventArgs e)
        // Create the custom editor.
        NumericUpDown editor = new NumericUpDown();
        editor.BorderStyle = BorderStyle.None;
        // Assign the custom editor to the grid.
        _flex.Cols[1].Editor = editor;

    Creating Custom Editors

    Any control that derives from the Control base class can be used as a grid editor. This is possible because the grid knows enough about the base class to access properties such as Text and Bounds, and events such as Leave and TextChanged. In many cases this level of support is adequate.

    In some cases, however, you may want to use controls that do not follow the base class that closely. For example, a DateTimePicker control has a Value property that should be used to retrieve the edited value instead of Text. In these cases, you can implement one or more methods in the IC1EmbeddedEditor interface to override the default behavior. For example, all controls in the C1Input library support IC1EmbeddedEditor and therefore integrate closely with C1FlexGrid (and also C1TrueDBGrid).

    The IC1EmbeddedEditor interface is fairly simple, and because the grid binds to it using late binding, you don't even have to implement all its members. Only implement the ones that make sense to your editor control.

    The interface does provide enough flexibility to allow virtually any control to be used as a grid editor. You can even use UITypeEditor classes as grid editors. To do this, you need a wrapper class that:

    1. Derives from Control (UITypeEditor doesn't).
    2. Implements the IC1EmbeddedEditor interface.
    3. Encapsulates the appropriate UITypeEditor.

    We provide source code for this wrapper class in the CustomEditors sample.

    Using the UITypeEditor wrapper class, you can use any UITypeEditors with the C1FlexGrid. .NET provides several UITypeEditors for editing colors, fonts, file names, and so on. You can also write your own UITypeEditors, in some cases that is easier than writing a control.

    Note: For an example of using built-in, custom, and UITypeEditor editors with C1FlexGrid, see the CustomEditors sample on GrapeCity website.
    See Also