Getting the Focused Row, Column and Cell in Developer Express XtraGrid

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

When testing an application that uses Developer Express XtraGrid controls, you may need to know which row (card), column (card field) and cell are currently focused. The XtraGrid control has special internal properties and methods that let you determine the currently selected element. These properties and methods are listed in the table below:

Property (Method) Description
GridObj.FocusedView Specifies the currently focused grid view. You can use this property, for example, to determine the focused view in the grid that displays data of multiple nested tables. If the grid displays data of one table only, this property is always equal to GridObj.MainView.
ViewObj.FocusedRowHandle Specifies the index (zero-based) of the row (card) that is currently selected in the given view.
ViewObj.FocusedColumn Specifies the column (card field) that is currently selected in the given view.
ViewObj.FocusedValue,
ViewObj.GetFocusedValue()
Returns the value in the currently selected cell.
ViewObj.GetFocusedDisplayText() Returns the text displayed in the currently selected cell.
In order for TestComplete to be able to access these properties and methods, the .NET Application Support plugin must be installed and enabled.

The sample code below demonstrates how you can use these properties to get the text of the focused cell. For more information on obtaining XtraGrid cell values, see Obtaining and Setting Cell Values in Developer Express XtraGrid.

The example works with the GridTutorials application.

How to get the application

JavaScript, JScript

function Test ()
{
  var p, frmMain, Grid;

  // Obtain the application process and its main form
  p = Sys.Process("GridTutorials");
  frmMain = p.WinFormsObject("frmMain");
  // Select the "Add New Row (Grouped Mode)" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Add New Row (Grouped Mode)";
  // Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Get information about the focused cell
  Log.Message ("Focused row: " + Grid.MainView.FocusedRowHandle)
  Log.Message ("Focused column: " + Grid.MainView.FocusedColumn.Caption.OleValue)
  Log.Message ("Focused cell's text: " + Grid.MainView.GetFocusedDisplayText().OleValue);
}

Python

def Test () :

  # Obtain the application process and its main form
  p = Sys.Process("GridTutorials")
  frmMain = p.WinFormsObject("frmMain")
  # Select the "Add New Row (Grouped Mode)" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Add New Row (Grouped Mode)"
  # Obtain the grid object
  Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")

  # Get information about the focused cell
  Log.Message ("Focused row: " + Grid.MainView.FocusedRowHandle)
  Log.Message ("Focused column: " + Grid.MainView.FocusedColumn.Caption.OleValue)
  Log.Message ("Focused cell's text: " + Grid.MainView.GetFocusedDisplayText().OleValue)

VBScript

Sub Test
  Dim p, frmMain, Grid

  ' Obtain the application process and its main form
  Set p = Sys.Process("GridTutorials")
  Set frmMain = p.WinFormsObject("frmMain")
  ' Select the "Add New Row (Grouped Mode)" demo
  frmMain.WinFormsObject("gcNavigations").WinFormsObject("listBoxControl1").SelectedItem = "Add New Row (Grouped Mode)"
  ' Obtain the grid object
  Set Grid = frmMain.WinFormsObject("pcMain").WinFormsObject("gcContainer").WinFormsObject("Form1").WinFormsObject("gridControl1")
 
  ' Get information about the focused cell
  Log.Message ("Focused row: " & Grid.MainView.FocusedRowHandle)
  Log.Message ("Focused column: " & Grid.MainView.FocusedColumn.Caption.OleValue)
  Log.Message ("Focused cell's text: " & Grid.MainView.GetFocusedDisplayText().OleValue)
End Sub

DelphiScript

procedure Test;
var p, frmMain, Grid : OleVariant;
begin;
  // Obtain the application process and its main form
  p := Sys.Process('GridTutorials');
  frmMain := p.WinFormsObject('frmMain');
  // Select the 'Add New Row (Grouped Mode)' demo
  frmMain.WinFormsObject('gcNavigations').WinFormsObject('listBoxControl1').SelectedItem := 'Add New Row (Grouped Mode)';
  // Obtain the grid object
  Grid := frmMain.WinFormsObject('pcMain').WinFormsObject('gcContainer').WinFormsObject('Form1').WinFormsObject('gridControl1');

  // Get information about the focused cell
  Log.Message ('Focused row: ' + aqConvert.VarToStr(Grid.MainView.FocusedRowHandle));
  Log.Message ('Focused column: ' + Grid.MainView.FocusedColumn.Caption.OleValue);
  Log.Message ('Selected cell''s text: ' + Grid.MainView.GetFocusedDisplayText.OleValue);
end;

C++Script, C#Script

function Test ()
{
  var p, frmMain, Grid;

  // Obtain the application process and its main form
  p = Sys["Process"]("GridTutorials");
  frmMain = p["WinFormsObject"]("frmMain");
  // Select the "Add New Row (Grouped Mode)" demo
  frmMain["WinFormsObject"]("gcNavigations")["WinFormsObject"]("listBoxControl1")["SelectedItem"] = "Add New Row (Grouped Mode)";
  // Obtain the grid object
  Grid = frmMain["WinFormsObject"]("pcMain")["WinFormsObject"]("gcContainer")["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");

  // Get information about the focused cell
  Log["Message"]("Focused row: " + Grid["MainView"]["FocusedRowHandle"])
  Log["Message"]("Focused column: " + Grid["MainView"]["FocusedColumn"]["Caption"]["OleValue"])
  Log["Message"]("Focused cell's text: " + Grid["MainView"]["GetFocusedDisplayText"]()["OleValue"]);
}

See Also

Working With Developer Express XtraGrid
Accessing Views in Developer Express XtraGrid
Obtaining and Setting Cell Values in Developer Express XtraGrid
Selecting Cells in Developer Express XtraGrid

Highlight search results