ComponentOne True DBGrid for WinForms
True DBGrid for WinForms Task-Based Help / Programmatically Entering Edit Mode
In This Topic
    Programmatically Entering Edit Mode
    In This Topic

    At run time cell edit mode is usually entered by the user's mouse and keyboard interaction with the grid. However, if you choose, you can set the currently focused cell to enter edit mode in code. To enter edit mode, simply set the EditActive property to True.

    In the following steps you'll add two labels and text boxes to your project to choose a cell to edit, a button to change focus to that cell, and another button that enters the focused cell into edit mode.

    Complete the following steps:

    1. Navigate to the Visual Studio Toolbox and add two Label controls and two TextBox controls to the form.
    2. Arrange Label1 next to TextBox1 and Label2 next to TextBox2 and, in the Properties window, set the following properties for the controls:
      • Set Label1.Text to "Column:".
      • Set TextBox1.Text to "0".
      • Set Label2.Text to "Row:".
      • Set TextBox2.Text to "0".
    3. Navigate to the Visual Studio Toolbox and add two Button controls to the form.
    4. Arrange the Button controls next to the Label and TextBox controls, and set the following properties in the Properties window:
      • Set Button11.Text to "Set Focus".
      • Set Button2.Text to "Edit Cell".
    5. Double-click Button1 to create the Click event handler and switch to code view.
    6. Add the following code to the Button1_Click event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1TrueDBGrid1.Col = Me.TextBox1.Text
      Me.C1TrueDBGrid1.Row = Me.TextBox2.Text
      

      To write code in C#

      C#
      Copy Code
      this.c1TrueDBGrid1.Col = this.textBox1.Text;
      this.c1TrueDBGrid1.Row = this.textBox2.Text;
      
    7. Return to Design view and double-click Button2 to create the Click event handler and switch to code view.
    8. Add the following code to the Button2_Click event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1TrueDBGrid1.EditActive = True
      

      To write code in C#

      C#
      Copy Code
      this.c1TrueDBGrid1.EditActive = true;
      

    What You've Accomplished

    Using the textboxes and buttons, you can change the cell that is in focus, and you can enter edit mode on the selected cell. Complete the following:

    1. Run your application.
    2. Change the values in the Column and Row text boxes, for example to "2" and "3", and click the Set Focus button.
      The focus of the grid changes and, if needed, the grid scrolls to bring the column and row in focus into view.
    3. Click the Edit Cell button.

    The selected cell will enter edit mode: