Activating and Closing In-place Editors in Microsoft DataGridView

Applies to TestComplete 15.70, last modified on December 17, 2024

Before entering data into the grid cell, you need to select it and activate its in-place editor, and after you have modified the cell value, you need to save changes. The different ways to select grid cells are described in the Selecting Cells in Microsoft DataGridView topic. In this topic, you will find information on how to activate the cell’s editor in order to be able to change the cell value, and how to close the editor saving the changes made.

To perform these actions, TestComplete should have access to internal objects, properties and methods of the DataGridView control. For this purpose, the .NET Application Support and Microsoft Control Support plugins must be installed and enabled.

When testing Microsoft DataGridView controls, use specific methods and properties of the corresponding MicrosoftDataGridView object. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with an object’s properties and methods from your scripts. However, when testing a DataGridView control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

Activating In-place Editors

In a DataGridView control, selecting a cell does not necessarily activate the edit mode. The way the cell’s editor can be activated depends on the grid’s EditMode property. To determine the value of this property, explore the tested application in the Object Browser.

Depending on the grid settings, you can activate the focused cell’s in-place editor in the following ways:

  • By clicking the focused cell.
  • By pressing the F2 key.
  • By typing into the focused cell. At that, the cell’s value is replaced by the value typed.
  • Using the grid’BeginEdit method.
Note: If the cell grid’s EditMode is EditOnEnter, the cell’s editor is automatically activated when the cell is clicked. In this case, no special actions are needed to put the cell into the edit mode.

The following ActivateCellEditor routine illustrates some of the described approaches that can be used to activate the cell’s editor.

JavaScript, JScript

function ActivateCellEditor (Grid)
{
  // Use any of the following statements --

  // Simulate the F2 shortcut
  Grid.Keys ("[F2]");

  // Click the selected cell
  // Grid.ClickCell (Grid.CurrentCellAddress.Y, Grid.CurrentCellAddress.X);

  // Call the BeginEdit method
  // Grid.BeginEdit (false);
}

Python

def ActivateCellEditor (Grid):
  # Use any of the following statements --

  # Simulate the F2 shortcut
  Grid.Keys ("[F2]")

  # Click the selected cell
  # Grid.ClickCell (Grid.CurrentCellAddress.Y, Grid.CurrentCellAddress.X)

  # Call the BeginEdit method
  # Grid.BeginEdit (False)

VBScript

Sub ActivateCellEditor (Grid)
  ' Use any of the following statements --

  ' Simulate the F2 shortcut
  Grid.Keys "[F2]"

  ' Click the selected cell
  ' Call Grid.ClickCell (Grid.CurrentCellAddress.Y, Grid.CurrentCellAddress.X)

  ' Call the BeginEdit method
  ' Grid.BeginEdit False
End Sub

DelphiScript

procedure ActivateCellEditor (Grid);
begin
  // Use any of the following statements --

  // Simulate the F2 shortcut
  Grid.Keys ('[F2]');

  // Click the selected cell
  // Grid.ClickCell (Grid.CurrentCellAddress.Y, Grid.CurrentCellAddress.X);

  // Call the BeginEdit method
  // Grid.BeginEdit (false);
end;

C++Script, C#Script

function ActivateCellEditor (Grid)
{
  // Use any of the following statements --

  // Simulate the F2 shortcut
  Grid["Keys"]("[F2]");

  // Click the selected cell
  // Grid["ClickCell"](Grid["CurrentCellAddress"]["Y"], Grid["CurrentCellAddress"]["X"]);

  // Call the BeginEdit method
  // Grid.BeginEdit (false);
}

Closing In-place Editors

After you have inputted the new cell value, you need to close the editor and save the changes made. You can do this in two ways:

  • By simulating the Enter key press.
  • By using the grid’s internal EndEdit method.

The following code snippet contains the CloseCellEditor routine that can be used to save changes made to a grid cell. The Grid parameter of this routine specifies the grid control under which the action is performed:

JavaScript, JScript

function CloseCellEditor (Grid)
{
  // Use any of the following statements --

  // Simulate the Enter key press
  Grid.Keys ("[Enter]");

  // Call the EndEdit method
  // Grid.EndEdit()
}

Python

def CloseCellEditor (Grid):
  # Use any of the following statements --

  # Simulate the Enter key press
  Grid.Keys ("[Enter]")

  # Call the EndEdit method
  # Grid.EndEdit()

VBScript

Sub CloseCellEditor (Grid)
  ' Use any of the following statements --

  ' Simulate the Enter key press
  Grid.Keys "[Enter]"

  ' Call the EndEdit method
  ' Grid.EndEdit
End Sub

DelphiScript

procedure CloseCellEditor (Grid);
begin
  //Use any of the following statements --

  // Simulate the Enter key press
  Grid.Keys ('[Enter]');

  // Call the EndEdit method
  // Grid.EndEdit;
end;

C++Script, C#Script

function CloseCellEditor (Grid)
{
  // Use any of the following statements --

  // Simulate the Enter key press
  Grid["Keys"]("[Enter]");

  // Call the EndEdit method
  // Grid["EndEdit"]()
}

You may also need to cancel the editing and discard any changes made to the cell value. For this purpose, you can either send the Esc key press to the grid control, or use the grid’ internal CancelEdit method. The CancelEditing routine in the example below demonstrates how to do this:

JavaScript, JScript

function CancelEditing (Grid)
{
  // Use any of the following statements --

  // Simulate the Esc key press
  Grid.Keys ("[Esc]");

  // Call the CancelEdit method
  // Grid.CancelEdit();
}

Python

def CancelEditing (Grid):
  # Use any of the following statements --

  # Simulate the Esc key press
  Grid.Keys ("[Esc]")

  # Call the CancelEdit method
  # Grid.CancelEdit()

VBScript

Sub CancelEditing (Grid)
  ' Use any of the following statements --

  ' Simulate the Esc key press
  Grid.Keys "[Esc]"

  ' Call the CancelEdit method
  ' Grid.CancelEdit
End Sub

DelphiScript

procedure CancelEditing (Grid);
begin
  // Use any of the following statements --

  // Simulate the Esc key press
  Grid.Keys ('[Esc]');

  // Call the CancelEdit method
  // Grid.CancelEdit;
end;

C++Script, C#Script

function CancelEditing (Grid)
{
  // Use any of the following statements --

  // Simulate the Esc key press
  Grid["Keys"]("[Esc]");

  // Call the CancelEdit method
  // Grid["CancelEdit"]();
}

See Also

Working With Microsoft DataGridView
Selecting Cells in Microsoft DataGridView
Obtaining and Setting Cell Values in Microsoft DataGridView

Highlight search results