ComponentOne True DBGrid for WinForms
True DBGrid for WinForms Task-Based Help / Moving the Focus in Code
In This Topic
    Moving the Focus in Code
    In This Topic

    At run time the grid cell's focus is usually determined by the user's mouse and keyboard interaction with the grid. However, if you choose to, you can set the column and row of the grid that has focus using the Col and Row properties of the grid.

    In this topic you'll add two NumbericUpDown controls to the form. When the value in those boxes changes, the column and row focus of the grid will change.

    Complete the following steps:

    1. Navigate to the Visual Studio Toolbox and add two Label controls and two NumberUpDown controls to the form.
    2. Arrange Label1 next to NumberUpDown1 and Label2 next to NumericUpDown2 and set the following properties for the controls:
      • Set Label1.Text to "Column:".
      • Set Label2.Text to "Row:".
    3. Double-click NumbericUpDown1 to create the ValueChanged event handler and switch to code view.
    4. Add the following code to the NumericUpDown1_ValueChanged event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1TrueDBGrid1.Col = Me.NumericUpDown1.Value
      

      To write code in C#

      C#
      Copy Code
      this.c1TrueDBGrid1.Col = this.numericUpDown1.Value;
      
    5. Return to Design view and double-click NumbericUpDown2 to create the ValueChanged event handler and switch to code view.
    6. Add the following code to the NumericUpDown2_ValueChanged event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1TrueDBGrid1.Row = Me.NumericUpDown2.Value
      

      To write code in C#

      C#
      Copy Code
      this.c1TrueDBGrid1.Row = this.numericUpDown2.Value;
      

    What You've Accomplished

    Change the value in the NumericUpDown boxes. Note that the focus of the grid changes and the grid scrolls to bring the column and row in focus into view: