Sorting Data in Developer Express QuantumGrid

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

The data displayed by the QuantumGrid control can be sorted by one or several columns. This topic describes the approaches that can be used to sort the grid data from test scripts:

In order for TestComplete to be able to perform these actions, the following conditions must be met: Note also, that the compiler can exclude methods and properties that are not called in the application’s source code from the application’s binary code, so these methods and properties are unavailable to TestComplete (see Object Properties, Fields and Methods That Are Unavailable to TestComplete). To solve the problem, make sure that the desired methods and properties are used in the application’s source code. For instance, you can add a virtual method to your application that calls the desired methods and properties (the compiler does not exclude virtual methods).

When testing Developer Express QuantumGrid controls, use specific methods and properties of the corresponding DevExpressQuantumGrid 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 QuantumGrid 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 Column Header Clicks

Typically, users specify the sorting column and change the direction (ascending or descending) by clicking the column caption. Subsequent clicks on the same header switch the sort direction. After a click, the previous sort conditions are replaced with sorting by the clicked column. In order to sort the data by this column preserving other sort conditions, you need to hold the Shift key while clicking the column header. To cancel sorting by a certain column, you should hold the Ctrl key while clicking its header. Thus, to sort a column in the ascending order, you should perform a Ctrl-click over the header and one ordinary click (or a Shift-click), and to sort the column in the descending order, a Ctrl-click, should be followed by two clicks.

You can simulate clicks over the QuantumGrid column headers using the ClickColumnHeader action of the DevExpressQuantumGrid object.

The example below demonstrates how you can sort data in the QuantumGrid control by simulating clicks on column headers.

JavaScript, JScript

function ColumnClickSorting()
{
  var p, Grid, ColumnName1, ColumnName2, ColumnName3;
  // Obtain the grid object
  p = Sys.Process("MySampleApp");
  Grid = p.VCLObject("FormName").VCLObject("GridName");
  ColumnName1 = "MyColumn1";
  ColumnName2 = "MyColumn2";
  ColumnName3 = "MyColumn3";

  // Sort the root level data
  Grid.ClickColumnHeader(ColumnName1);
  Grid.ClickColumnHeader(ColumnName2, 0, skShift);
  // Display the child table of the first row
  Grid.Expand(1);
  // Sort the child table data
  Grid.wChildLevel(1, 0).ClickColumnHeader(ColumnName3, 1);
  // Cancel sorting by the first column
  Grid.ClickColumnHeader(ColumnName1, 0, skCtrl);
}

Python

def ColumnClickSorting():
  # Obtain the grid object
  p = Sys.Process("MySampleApp")
  Grid = p.VCLObject("FormName").VCLObject("GridName")
  ColumnName1 = "MyColumn1"
  ColumnName2 = "MyColumn2"
  ColumnName3 = "MyColumn3"

  # Sort the root level data
  Grid.ClickColumnHeader(ColumnName1)
  Grid.ClickColumnHeader(ColumnName2, 0, skShift)
  # Display the child table of the first row 
  Grid.Expand(1)
  # Sort the child table data
  Grid.wChildLevel[1, 0].ClickColumnHeader(ColumnName3, 1)
  # Cancel sorting by the first column
  Grid.ClickColumnHeader(ColumnName1, 0, skCtrl)

VBScript

Sub ColumnClickSorting
  Dim p, Grid, ColumnName1, ColumnName2, ColumnName3
  ' Obtain the grid object
  Set p = Sys.Process("MySampleApp")
  Set Grid = p.VCLObject("FormName").VCLObject("GridName")
  ColumnName1 = "MyColumn1"
  ColumnName2 = "MyColumn2"
  ColumnName3 = "MyColumn3"

  ' Sort the root level data
  Call Grid.ClickColumnHeader(ColumnName1)
  Call Grid.ClickColumnHeader(ColumnName2, 0, skShift)
  ' Display the child table of the first row
  Call Grid.Expand(1)
  ' Sort the child table data
  Call Grid.wChildLevel(1, 0).ClickColumnHeader(ColumnName3, 1)
  ' Cancel sorting by the first column
  Call Grid.ClickColumnHeader(ColumnName1, 0, skCtrl)
End Sub

DelphiScript

procedure ColumnClickSorting;
var p, ColumnName1, ColumnName2, ColumnName3, Grid: OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('MySampleApp');
  Grid := p.VCLObject('FormName').VCLObject('GridName');
  ColumnName1 := 'MyColumn1';
  ColumnName2 := 'MyColumn2';
  ColumnName3 := 'MyColumn3';

  // Sort the root level data
  Grid.ClickColumnHeader(ColumnName1);
  Grid.ClickColumnHeader(ColumnName2, 0, skShift);
  // Display the child table of the first row
  Grid.Expand(1);
  // Sort the child table data
  Grid.wChildLevel(1, 0).ClickColumnHeader(ColumnName3, 1);
  // Cancel sorting by the first column
  Grid.ClickColumnHeader(ColumnName1, 0, skCtrl);
end;

C++Script, C#Script

function ColumnClickSorting()
{
  var p, Grid, ColumnName1, ColumnName2, ColumnName3;
  // Obtain the grid object
  p = Sys["Process"]("MySampleApp");
  Grid = p["VCLObject"]("FormName")["VCLObject"]("GridName");
  ColumnName1 = "MyColumn1";
  ColumnName2 = "MyColumn2";
  ColumnName3 = "MyColumn3";
  

  // Sort the root level data
  Grid["ClickColumnHeader"](ColumnName1);
  Grid["ClickColumnHeader"](ColumnName2, 0, skShift);
  // Display the child table of the first row
  Grid["Expand"](1);
  // Sort the child table data
  Grid["wChildLevel"](1, 0)["ClickColumnHeader"](ColumnName3, 1);
  // Cancel sorting by the first column
  Grid["ClickColumnHeader"](ColumnName1, 0, skCtrl);
}

Using Internal Members of QuantumGrid

Alternatively, you can sort grid data using the internal properties of the TcxGridColumn object that represents a grid column. This approach is more complicated, but using it you can find out the initial sort order of a column.

To obtain the TcxGridColumn object that corresponds to the desired column, you can use the GetColumn routine that is described in the Obtaining and Setting Cell Values in Developer Express QuantumGrid topic. It retrieves a column specified by the caption or by the visible position in a grid.

The TcxGridColumn object has the SortOrder that defines the current sort order of a column. The property can accept the following string values:

Value Description
1 Ascending
2 Descending
0 No sorting

Note that assigning 1 or 2 to the SortOrder property does not automatically cancel sorting by other column(s).

Example

View description

JavaScript

function Main()
{
  var p, Grid, ColumnName1, ColumnName2, ColumnName3;

  // Obtain the grid object
  p = Sys.Process("MySampleApp");
  Grid = p.VCLObject("FormName").VCLObject("GridName");
  ColumnName1 = "MyColumn1";
  ColumnName2 = "MyColumn2";
  ColumnName3 = "MyColumn3";

  // Sort the grid data
  SortAscending (Grid, null, ColumnName1);
  SortDescending (Grid, null, ColumnName2);
  SortAscending (Grid, null, ColumnName3);
  CancelSorting (Grid, null, ColumnName1);
}

function SortAscending (Grid, View, ColumnId)
{
  // Get the column object
  let Column = GetColumn (Grid, View, ColumnId);

  // Set the ascending order
  Column.SortOrder = 1;
}

function SortDescending (Grid, View, ColumnId)
{
  // Get the column object
  let Column = GetColumn (Grid, View, ColumnId);

  // Set the descending order
  Column.SortOrder = 2;
}

function CancelSorting (Grid, View, ColumnId)
{
  // Get the column object
  let Column = GetColumn (Grid, View, ColumnId);

  // Cancel sorting
  Column.SortOrder = 0;
}

// Obtain the column object
function GetColumn (Grid, View, ColumnId)
{
  // Obtain the view object
  if (strictEqual(View, null))
    View = Grid.ActiveView;

  // Check type of the columnId parameter
  if (equal(aqObject.GetVarType(ColumnId), varOleStr))
  {
    // If ColumnId is a string,
    // then search for the column by its caption
    for (let i = 0; i < View.ColumnCount; i++)
      if (equal(View.GetColumn(i).Caption, ColumnId))
        return View.GetColumn(i); // Column is found

    return null; // Column is not found
  }
  else
    // If ColumnId is an integer,
    // then search for column by its index
    return View.GetColumn(ColumnId);
}

JScript

function Main()
{
  var p, Grid, ColumnName1, ColumnName2, ColumnName3;

  // Obtain the grid object
  p = Sys.Process("MySampleApp");
  Grid = p.VCLObject("FormName").VCLObject("GridName");
  ColumnName1 = "MyColumn1";
  ColumnName2 = "MyColumn2";
  ColumnName3 = "MyColumn3";

  // Sort the grid data
  SortAscending (Grid, null, ColumnName1);
  SortDescending (Grid, null, ColumnName2);
  SortAscending (Grid, null, ColumnName3);
  CancelSorting (Grid, null, ColumnName1);
}

function SortAscending (Grid, View, ColumnId)
{
  // Get the column object
  var Column = GetColumn (Grid, View, ColumnId);

  // Set the ascending order
  Column.SortOrder = 1;
}

function SortDescending (Grid, View, ColumnId)
{
  // Get the column object
  var Column = GetColumn (Grid, View, ColumnId);

  // Set the descending order
  Column.SortOrder = 2;
}

function CancelSorting (Grid, View, ColumnId)
{
  // Get the column object
  var Column = GetColumn (Grid, View, ColumnId);

  // Cancel sorting
  Column.SortOrder = 0;
}

// Obtain the column object
function GetColumn (Grid, View, ColumnId)
{
  // Obtain the view object
  if (View == null)
    View = Grid.ActiveView;

  // Check type of the columnId parameter
  if (aqObject.GetVarType(ColumnId) == varOleStr)
  {
    // If ColumnId is a string,
    // then search for the column by its caption
    for (var i = 0; i < View.ColumnCount; i++)
      if (View.GetColumn(i).Caption == ColumnId)
        return View.GetColumn(i); // Column is found

    return null; // Column is not found
  }
  else
    // If ColumnId is an integer,
    // then search for column by its index
    return View.GetColumn(ColumnId);
}

Python

def Main():

  # Obtain the grid object
  p = Sys.Process("MySampleApp")
  Grid = p.VCLObject("FormName").VCLObject("GridName")

  # Sort the grid data
  ColumnName1 = "MyColumn1"
  ColumnName2 = "MyColumn2"
  ColumnName3 = "MyColumn3"
  SortAscending (Grid, None, ColumnName1)
  SortDescending (Grid, None, ColumnName2)
  SortAscending (Grid, None, ColumnName3)
  CancelSorting (Grid, None, ColumnName1)

def SortAscending (Grid, View, ColumnId):
  # Get the column object 
  Column = GetColumn (Grid, View, ColumnId)

  # Set the ascending order
  Column.SortOrder = 1

def SortDescending (Grid, View, ColumnId):
  # Get the column object
  Column = GetColumn (Grid, View, ColumnId)

  # Set the descending order
  Column.SortOrder = 2

def CancelSorting (Grid, View, ColumnId):
  # Get the column object 
  Column = GetColumn (Grid, View, ColumnId)

  # Cancel sorting
  Column.SortOrder = 0

# Obtain the column object
def GetColumn (Grid, View, ColumnId):
  # Obtain the view object 
  if (View == None):
    View = Grid.ActiveView

  # Check type of the columnId parameter 
  if (aqObject.GetVarType(ColumnId) == varOleStr):
    # If ColumnId is a string,
    # then search for the column by its caption
    for i in range(0, View.ColumnCount-1):
      if (View.GetColumn[i].Caption == ColumnId):
        return View.GetColumn[i] # Column is found

    return None # Column is not found
  else:
    # If ColumnId is an integer,
    # then search for column by its index
    return View.GetColumn(ColumnId)

VBScript

Sub Main
  Dim p, Grid, ColumnName1, ColumnName2, ColumnName3
  ' Obtain the grid object
  Set p = Sys.Process("MySampleApp")
  Set Grid = p.VCLObject("FormName").VCLObject("GridName")
  ColumnName1 = "MyColumn1"
  ColumnName2 = "MyColumn2"
  ColumnName3 = "MyColumn3"
  
  ' Sort grid data
  Call SortAscending (Grid, Nothing, ColumnName1)
  Call SortDescending (Grid, Nothing, ColumnName2)
  Call SortAscending (Grid, Nothing, ColumnName3)
  Call CancelSorting (Grid, Nothing, ColumnName1)
End Sub

Sub SortAscending (Grid, View, ColumnId)
  Dim Column

  ' Get the column object
  Set Column = GetColumn (Grid, View, ColumnId)

  ' Set the ascending order
  Column.SortOrder = 1
End Sub 

Sub SortDescending (Grid, View, ColumnId)
  Dim Column

  ' Get the column object
  Set Column = GetColumn (Grid, View, ColumnId)

  ' Set the descending order
  Column.SortOrder = 2
End Sub 

Sub CancelSorting (Grid, View, ColumnId)
  Dim Column

  ' Get the column object
  Set Column = GetColumn (Grid, View, ColumnId)

  ' Cancel sorting
  Column.SortOrder = 0
End Sub

' Obtains the column object
Function GetColumn (Grid, View, ColumnId)
  Dim i

  ' Obtain the view object
  If View Is Nothing Then
    Set View = Grid.ActiveView
  End If

  ' Check type of the columnId parameter
  If aqObject.GetVarType(ColumnId) = varOleStr Then
    ' If ColumnId is a string, search for the column by its caption
    For i = 0 to View.ColumnCount-1
      If View.GetColumn(i).Caption = ColumnId Then
        Set GetColumn = View.GetColumn(i) ' Column is found
        Exit Function
      End If
    Next

    Set GetColumn = Nothing' Column is not found
  Else
    ' If ColumnId is an integer, get the column by its index
    Set GetColumn = View.GetColumn(ColumnId)
  End If
End Function

DelphiScript

procedure SortAscending (Grid, View, ColumnId); forward;
procedure SortDescending (Grid, View, ColumnId); forward;
procedure CancelSorting (Grid, View, ColumnId); forward;
function GetColumn (Grid, View, ColumnId); forward;

procedure Main;
var p, ColumnName1, ColumnName2, ColumnName3, Grid : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process('MySampleApp');
  Grid := p.VCLObject('FormName').VCLObject('GridName');
  ColumnName1 := 'MyColumn1';
  ColumnName2 := 'MyColumn2';
  ColumnName3 := 'MyColumn3';

  // the Sort grid data
  SortAscending (Grid, nil, ColumnName1);
  SortDescending (Grid, nil, ColumnName2);
  SortAscending (Grid, nil, ColumnName3);
  CancelSorting (Grid, nil, ColumnName1);
end;

procedure SortAscending (Grid, View, ColumnId);
var Column: OleVariant;
begin
  // Get the column object
  Column := GetColumn (Grid, View, ColumnId);

  // Set the ascending order
  Column.SortOrder := 1;
end;

procedure SortDescending (Grid, View, ColumnId);
var Column: Variant;
begin
  // Get the column object
  Column := GetColumn (Grid, View, ColumnId);

  // Set the descending order
  Column.SortOrder := 2;
end;

procedure CancelSorting (Grid, View, ColumnId);
var Column: Variant;
begin
  // Get the column object
  Column := GetColumn (Grid, View, ColumnId);

  // Cancel sorting
  Column.SortOrder := 0;
end;

// Obtain the column object
function GetColumn (Grid, View, ColumnId);
var i : OleVariant;
begin
  // Obtain the view object
  if View = nil then
    View := Grid.ActiveView;

  // Check type of the columnId parameter
  if aqObject.GetVarType(ColumnId) = varOleStr then
  begin
    // If ColumnId is a string, search for the column by its caption
    for i := 0 to View.ColumnCount-1 do
      if View.GetColumn(i).Caption = ColumnId then
      begin
        Result := View.GetColumn(i); // Column is found
        Exit;
      end;

    Result := nil; // Column is not found
  end
  else
    // If ColumnId is an integer, get the column by its index
    Result := View.GetColumn(ColumnId);
end;

C++Script, C#Script

function Main()
{
  var p, Grid, ColumnName1, ColumnName2, ColumnName3;

  // Obtain the grid object
  p = Sys["Process"]("MySampleApp");
  Grid = p["VCLObject"]("FormName")["VCLObject"]("GridName");
  ColumnName1 = "MyColumn1";
  ColumnName2 = "MyColumn2";
  ColumnName3 = "MyColumn3";

  // Sort the grid data
  SortAscending (Grid, null, ColumnName1);
  SortDescending (Grid, null, ColumnName2);
  SortAscending (Grid, null, ColumnName3);
  CancelSorting (Grid, null, ColumnName1);
}

function SortAscending (Grid, View, ColumnId)
{
  // Get the column object
  var Column = GetColumn (Grid, View, ColumnId);

  // Set the ascending order
  Column["SortOrder"] = 1;
}

function SortDescending (Grid, View, ColumnId)
{
  // Get the column object
  var Column = GetColumn (Grid, View, ColumnId);

  // Set the descending order
  Column["SortOrder"] = 2;
}

function CancelSorting (Grid, View, ColumnId)
{
  // Get the column object
  var Column = GetColumn (Grid, View, ColumnId);

  // Cancel sorting
  Column["SortOrder"] = 0;
}

// Obtain the column object
function GetColumn (Grid, View, ColumnId)
{
  // Obtain the view object
  if (View == null)
    View = Grid["ActiveView"];

  // Check type of the columnId parameter
  if (aqObject["GetVarType"](ColumnId) == varOleStr)
  {
    // If ColumnId is a string, search for the column by its caption
    for (var i = 0; i < View["ColumnCount"]; i++)
      if (View["GetColumn"](i)["Caption"] == ColumnId)
        return View["GetColumn"](i); // Column is found

    return null; // Column is not found
  }
  else
    // If ColumnId is an integer, search for column by its index
    return View["GetColumn"](ColumnId);
}

See Also

Working With Developer Express QuantumGrid
Obtaining and Setting Cell Values in Developer Express QuantumGrid
ClickColumnHeader Action (Specific to DevExpressQuantumGrid Controls)

Highlight search results