Getting the Focused Row, Column and Cell in Microsoft DataGridView

Applies to TestComplete 15.63, last modified on April 10, 2024

When testing an application that uses Microsoft DataGridView controls, you may need to know which row, column and cell are currently focused. The DataGridView control has special internal properties that let you determine the currently selected element:

  • CurrentCellAddress.Y and CurrentCell.RowIndex - Return the index (zero-based) of the selected row.
  • CurrentCellAddress.X and CurrentCell.ColumnIndex - Return the index (zero-based) of the currently selected column.
  • CurrentCell.Value - Returns the value in the focused cell.
In order for TestComplete to be able to access these properties, the .NET Application Support plugin must be installed and enabled.

The following example demonstrates how you can use these properties to get the value in the focused cell. For more information on obtaining DataGridView cell values, see Obtaining and Setting Cell Values in Microsoft DataGridView.

JavaScript

function Test ()
{
  var p, Grid, RowIndex, ColIndex, CellValue;

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

  // Get indexes of the focused row and column
  RowIndex = Grid.CurrentCellAddress.Y;
  ColIndex = Grid.CurrentCellAddress.X;
  Log.Message ("Focused row: " + RowIndex);
  Log.Message ("Focused column: " + Grid.wColumn(ColIndex));

  // Get the value in the focused cell
  CellValue = Grid.CurrentCell.Value;
  // You can also get the cell value using the MicrosoftDataGridView.wValue property:
  // CellValue = Grid.wValue (RowIndex, ColIndex);
  if (equal(aqObject.GetVarType (CellValue), varDispatch))
    Log.Message ("Focused cell value: " + CellValue.ToString().OleValue)
  else
    Log.Message ("Focused cell value: " + CellValue);
}

JScript

function Test ()
{
  var p, Grid, RowIndex, ColIndex, CellValue;

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

  // Get indexes of the focused row and column
  RowIndex = Grid.CurrentCellAddress.Y;
  ColIndex = Grid.CurrentCellAddress.X;
  Log.Message ("Focused row: " + RowIndex);
  Log.Message ("Focused column: " + Grid.wColumn(ColIndex));

  // Get the value in the focused cell
  CellValue = Grid.CurrentCell.Value;
  // You can also get the cell value using the MicrosoftDataGridView.wValue property:
  // CellValue = Grid.wValue (RowIndex, ColIndex);
  if (aqObject.GetVarType (CellValue) == varDispatch)
    Log.Message ("Focused cell value: " + CellValue.ToString().OleValue)
  else
    Log.Message ("Focused cell value: " + CellValue);
}

Python

def Test ():

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

  # Get indexes of the focused row and column
  RowIndex = Grid.CurrentCellAddress.Y
  ColIndex = Grid.CurrentCellAddress.X
  Log.Message ("Focused row: " + RowIndex)
  Log.Message ("Focused column: " + Grid.wColumn(ColIndex))

  # Get the value in the focused cell
  CellValue = Grid.CurrentCell.Value
  # You can also get the cell value using the MicrosoftDataGridView.wValue property:
  # CellValue = Grid.wValue (RowIndex, ColIndex)
  if (aqObject.GetVarType (CellValue) == varDispatch):
    Log.Message ("Focused cell value: " + CellValue.ToString().OleValue)
  else:
    Log.Message ("Focused cell value: " + CellValue)

VBScript

Sub Test
  Dim p, Grid, RowIndex, ColIndex, CellValue

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

  ' Get indexes of the focused row and column
  RowIndex = Grid.CurrentCellAddress.Y
  ColIndex = Grid.CurrentCellAddress.X
  Log.Message ("Focused row: " & RowIndex)
  Log.Message ("Focused column: " & Grid.wColumn(ColIndex))

  ' Get the value in the focused cell
  If aqObject.GetVarType (Grid.CurrentCell.Value) = varDispatch Then
    Set CellValue = Grid.CurrentCell.Value
    ' You can also get the cell value using the MicrosoftDataGridView.wValue property:
    ' Set CellValue = Grid.wValue (RowIndex, ColIndex);
    Log.Message "Focused cell value: " & CellValue.ToString.OleValue
  Else
    CellValue = Grid.CurrentCell.Value
    ' -- or --
    ' CellValue = Grid.wValue (RowIndex, ColIndex)
    Log.Message "Focused cell value: " & CellValue
  End If
End Sub

DelphiScript

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

  // Get indexes of the focused row and column
  RowIndex := Grid.CurrentCellAddress.Y;
  ColIndex := Grid.CurrentCellAddress.X;
  Log.Message ('Focused row: ' + aqConvert.VarToStr(RowIndex));
  Log.Message ('Focused column: ' + Grid.wColumn(ColIndex));

  // Get the value in the focused cell
  CellValue := Grid.CurrentCell.Value;
  // You can also get the cell value using the MicrosoftDataGridView.wValue property:
  // CellValue := Grid.wValue[RowIndex, ColIndex];
  if aqObject.GetVarType(CellValue) = varDispatch then
    Log.Message ('Focused cell value: ' + CellValue.ToString.OleValue)
  else
    Log.Message ('Focused cell value: ' + aqConvert.VarToStr(CellValue));
end;

C++Script, C#Script

function Test ()
{
  var p, Grid, RowIndex, ColIndex, CellValue;

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

  // Get indexes of the focused row and column
  RowIndex = Grid["CurrentCellAddress"]["Y"];
  ColIndex = Grid["CurrentCellAddress"]["X"];
  Log["Message"]("Focused row: " + RowIndex);
  Log["Message"]("Focused column: " + Grid["wColumn"](ColIndex));

  // Get the value in the focused cell
  CellValue = Grid["CurrentCell"]["Value"];
  // You can also get the cell value using the MicrosoftDataGridView.wValue property:
  // CellValue = Grid["wValue"](RowIndex, ColIndex);
  if (aqObject["GetVarType"] (CellValue) == varDispatch)
    Log["Message"]("Focused cell value: " + CellValue["ToString"]()["OleValue"])
  else
    Log["Message"]("Focused cell value: " + CellValue);
}

See Also

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

Highlight search results