Selecting Cells in Microsoft DataGrid

Applies to TestComplete 14.40, last modified on April 22, 2021

One of the actions that you will perform the most over a grid is selecting cells. This topic describes several approaches that can be used to select a particular cell in Microsoft DataGrid .NET controls:

Simulating Clicks on Cells

You can simulate clicks on grid cells using the ClickCell, ClickCellR, DblClickCell and similar actions of the MisrosoftDataGrid object. All of these actions have parameters that specify the row and column that contain the desired cell. They also have an additional parameter that specifies the key or a combination of keys (Ctrl, Alt, Shift) that are pressed during the simulation of a click.

Note: TestComplete extends the DataGrid control functionality with properties and methods of the MicrosoftDataGrid object only if the Microsoft Control Support plugin is installed and enabled.

Below is an example that demonstrates how you can simulate clicks on grid cells using the ClickCell action:

JavaScript, JScript

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys.Process ("DataGridSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1");

  // Click some cells
  Grid.ClickCell (2, "Customer Name");
  Grid.ClickCell (2, "Product");
  Grid.ClickCell (2, "Quantity");
}

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process ("DataGridSample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1")

  # Click some cells
  Grid.ClickCell (2, "Customer Name")
  Grid.ClickCell (2, "Product")
  Grid.ClickCell (2, "Quantity")

VBScript

Sub Main
  Dim p, Grid

  ' Obtain the grid object
  Set p = Sys.Process ("DataGridSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("dataGrid1")

  ' Click some cells
  Call Grid.ClickCell (2, "Customer Name")
  Call Grid.ClickCell (2, "Product")
  Call Grid.ClickCell (2, "Quantity")
End Sub

DelphiScript

procedure Main;
var p, Grid : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process ('DataGridSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('dataGrid1');

  // Click some cells
  Grid.ClickCell (2, 'Customer Name');
  Grid.ClickCell (2, 'Product');
  Grid.ClickCell (2, 'Quantity');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

  // Obtain the grid object
  p = Sys["Process"]("DataGridSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("dataGrid1");

  // Click some cells
  Grid["ClickCell"](2, "Customer Name");
  Grid["ClickCell"](2, "Product");
  Grid["ClickCell"](2, "Quantity");
}

Simulating Keyboard Shortcuts

The DataGrid control supports various keyboard shortcuts that can be used to navigate through the grid. For example, pressing an arrow key selects the neighbor cell in the direction of the arrow, and Ctrl+Home navigates to the grid’s upper-left cell.

For more information on what shortcuts can be used in the DataGrid control, see docs.microsoft.com/en-us/dotnet/framework/winforms/controls/keyboard-shortcuts-for-the-windows-forms-datagrid-control.

To simulate a keyboard shortcut, use the Keys action of the grid control. The following code snippet simulates the Ctrl+Home shortcut to navigate to the grid’s upper left cell:

JavaScript, JScript

Grid = Sys.Process("DataGridSample").WinFormsObject("Form1").WinFormsObject("dataGrid1");
Grid.Keys ("^[Home]");

Python

Grid = Sys.Process("DataGridSample").WinFormsObject("Form1").WinFormsObject("dataGrid1")
Grid.Keys ("^[Home]")

VBScript

Set Grid = Sys.Process("DataGridSample").WinFormsObject("Form1").WinFormsObject("dataGrid1")
Grid.Keys "^[Home]"

DelphiScript

Grid := Sys.Process('DataGridSample').WinFormsObject('Form1').WinFormsObject('dataGrid1');
Grid.Keys ('^[Home]');

C++Script, C#Script

Grid = Sys["Process"]("DataGridSample")["WinFormsObject"]("Form1")["WinFormsObject"]("dataGrid1");
Grid["Keys"]("^[Home]");

See Also

Working With Microsoft DataGrid
ClickCell Action (Grid Controls)
Getting the Focused Row, Column and Cell in Microsoft DataGrid
Obtaining and Setting Cell Values in Microsoft DataGrid

Highlight search results