Selecting Cells in Microsoft DataGridView

Applies to TestComplete 15.47, last modified on January 20, 2023

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 the DataGridView control:

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.

Simulating Clicks on Cells

You can simulate clicks on grid cells using the ClickCell, ClickCellR, DblClickCell and similar actions of the MisrosoftDataGridView 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.

Below is an example that demonstrates how you can simulate clicks on grid cells:

JavaScript, JScript

function Main ()
{
  var p, Grid;

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

  // 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 ("DataGridViewSample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1")

  # 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 ("DataGridViewSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("dataGridView1")

  ' 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 ('DataGridViewSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('dataGridView1');

  // 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"]("DataGridViewSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("dataGridView1");

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

Simulating Keyboard Shortcuts

The DataGridView 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 DataGridView control, see the Default Keyboard and Mouse Handling in the Windows Forms DataGridView Control topic in MSDN.

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("DataGridViewSample").WinFormsObject("Form1").WinFormsObject("dataGridView1");
Grid.Keys ("^[Home]");

Python

Grid = Sys.Process("DataGridViewSample").WinFormsObject("Form1").WinFormsObject("dataGridView1")
Grid.Keys ("^[Home]")

VBScript

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

DelphiScript

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

C++Script, C#Script

Grid = Sys["Process"]("DataGridViewSample")["WinFormsObject"]("Form1")["WinFormsObject"]("dataGridView1");
Grid["Keys"]("^[Home]");

Using the DataGridView.CurrentCell Property

It is possible to select a particular cell in the DataGridView control using its native properties and methods. The DataGridView control has the CurrentCell property which can be used to determine or specify the cell focused in the grid. When setting this property to a cell which is not currently visible, the grid automatically scrolls in order for the cell to become visible. This property accepts and returns the DataGridCell object that represents a grid cell. The following code demonstrates how to use the CurrentCell property to select a grid cell.

Example

View description

JavaScript

function Main ()
{
  var p, Grid;

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

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

// Selects the specified cell
function SelectCell (Grid, RowIndex, ColumnId)
{
  // Get the cell's column index
  let ColIndex = GetColIndexById (Grid, ColumnId);
  // Select the cell
  Grid.CurrentCell = Grid.Item(ColIndex, RowIndex);
}

// Returns the column index by its identifier
function GetColIndexById (Grid, ColumnId)
{
  // Check if the column is specified by caption or index
  if (equal(aqObject.GetVarType(ColumnId), varOleStr))
  {
    // Search for the column by its caption
    for (let i=0; i<Grid.ColumnCount; i++)
      if (equal(Grid.Columns.Item(i).HeaderText.OleValue, ColumnId))
        return i; // Column is found

    return -1;    // Column is not found
  }
  else
    // The column is specified by index; return this index
    return ColumnId;
}

JScript

function Main ()
{
  var p, Grid;

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

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

// Selects the specified cell
function SelectCell (Grid, RowIndex, ColumnId)
{
  // Get the cell's column index
  var ColIndex = GetColIndexById (Grid, ColumnId);
  // Select the cell
  Grid.CurrentCell = Grid.Item(ColIndex, RowIndex);
}

// Returns the column index by its identifier
function GetColIndexById (Grid, ColumnId)
{
  // Check if the column is specified by caption or index
  if (aqObject.GetVarType(ColumnId) == varOleStr)
  {
    // Search for the column by its caption
    for (i=0; i<Grid.ColumnCount; i++)
      if (Grid.Columns.Item(i).HeaderText.OleValue == ColumnId)
        return i; // Column is found

    return -1;    // Column is not found
  }
  else
    // The column is specified by index; return this index
    return ColumnId;
}

Python

def Main ():

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

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

# Selects the specified cell
def SelectCell (Grid, RowIndex, ColumnId):
  # Get the cell's column index
  ColIndex = GetColIndexById (Grid, ColumnId)
  # Select the cell
  Grid.CurrentCell = Grid.Item(ColIndex, RowIndex)

# Returns the column index by its identifier
def GetColIndexById (Grid, ColumnId):
  # Check if the column is specified by caption or index
  if (aqObject.GetVarType(ColumnId) == varOleStr):
    # Search for the column by its caption
    for i in range(0, Grid.ColumnCount-1):
      if (Grid.Columns.Item[i].HeaderText.OleValue == ColumnId):
        return i # Column is found

    return -1    # Column is not found
  else:
    # The column is specified by index return this index
    return ColumnId

VBScript

Function Main ()
  Dim p, Grid

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

  ' Click some cells
  Call SelectCell (Grid, 2, "Customer Name")
  Call SelectCell (Grid, 2, "Product")
  Call SelectCell (Grid, 2, "Quantity")
End Function

' Selects the specified cell
Function SelectCell (Grid, RowIndex, ColumnId)
  ' Get the cell's column index
  Dim ColIndex
  ColIndex = GetColIndexById (Grid, ColumnId)
  ' Select the cell
  Set Grid.CurrentCell = Grid.Item(ColIndex, RowIndex)
End Function

' Returns the column index by its identifier
Function GetColIndexById (Grid, ColumnId)
  ' Check if the column is specified by caption or index
  If aqObject.GetVarType(ColumnId) = varOleStr Then
  
    ' Search for the column by its caption
    For i=0 To Grid.ColumnCount-1
      If Grid.Columns.Item(i).HeaderText.OleValue = ColumnId Then
        GetColIndexById = i ' Column is found
        Exit Function
      End If
    Next
    GetColIndexById = -1    ' Column is not found
  Else
    ' The column is specified by index return this index
    GetColIndexById = ColumnId
  End If
End Function

DelphiScript

procedure SelectCell (Grid, RowIndex, ColumnId); forward;
function GetColIndexById (Grid, ColumnId); forward;

procedure Main_SelectCell;
var p, Grid : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process ('DataGridViewSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('dataGridView1');

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

// Selects the specified cell
procedure SelectCell (Grid, RowIndex, ColumnId);
var ColIndex : OleVariant;
begin
  // Get the cell's column index
  ColIndex := GetColIndexById (Grid, ColumnId);
  // Select the cell
  Grid.CurrentCell := Grid.Item[ColIndex, RowIndex];
end;

// Returns the column index by its identifier
function GetColIndexById (Grid, ColumnId);
var i: OleVariant;
begin
  // Check if the column is specified by caption or index
  if aqObject.GetVarType(ColumnId) = varOleStr then
  begin
    // Search for the column by its caption
    for i:=0 to Grid.ColumnCount-1 do
      if Grid.Columns.Item[i].HeaderText.OleValue = ColumnId then
      begin
        Result := i; // Column is found
        Exit;
      end;

    Result := -1;    // Column is not found
  end
  else
    // The column is specified by index; return this index
    Result := ColumnId;
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

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

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

// Selects the specified cell
function SelectCell (Grid, RowIndex, ColumnId)
{
  // Get the cell's column index
  var ColIndex = GetColIndexById (Grid, ColumnId);
  // Select the cell
  Grid["CurrentCell"] = Grid["Item"](ColIndex, RowIndex);
}

// Returns the column index by its identifier
function GetColIndexById (Grid, ColumnId)
{
  // Check if the column is specified by caption or index
  if (aqObject["GetVarType"](ColumnId) == varOleStr)
  {
    // Search for the column by its caption
    for (i=0; i<Grid["ColumnCount"]; i++)
      if (Grid["Columns"]["Item"](i)["HeaderText"]["OleValue"] == ColumnId)
        return i; // Column is found

    return -1;    // Column is not found
  }
  else
    // The column is specified by index; return this index
    return ColumnId;
}

See Also

Working With Microsoft DataGridView
ClickCell Action (Grid Controls)
Getting the Focused Row, Column and Cell in Microsoft DataGridView
Activating and Closing In-place Editors in Microsoft DataGridView
Obtaining and Setting Cell Values in Microsoft DataGridView

Highlight search results