Sorting Data in Syncfusion GridGroupingControl

Applies to TestComplete 15.0, last modified on October 22, 2021

Syncfusion GridGroupingControl lets the end users sort the displayed data. This topic describes the approaches that can be used to sort the grid data from test scripts:

To perform these actions, TestComplete should have access to internal objects, properties and methods of the GridGroupingControl object. For this purpose, the .NET Application Support and Syncfusion Systems Control Support plugins must be installed and enabled. The latter lets you work with the GridGroupingControl controls using methods and properties of the SyncfusionEssGrid object. Without this plugin, you will not be able to work with controls using their internal methods and properties.

When testing Syncfusion GridGroupingControl controls, use specific methods and properties of the corresponding SyncfusionEssGrid 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 GridGroupingControl 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

It is possible to sort the data displayed in the GridGroupingControl l by clicking on the header of the column that you want to sort. The next click on the same header switches the sort direction (from ascending to descending, and vice versa). 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 should hold the Ctrl key while clicking the column header.

With TestComplete, you can simulate clicks as well as Shift- and Ctrl-clicks on the GridGroupingControl column headers using the ClickColumnHeader action of the SyncfusionEssGrid or SyncfusionEssGridView object. The following example demonstrates how you can do it:

JavaScript, JScript

function Main ()
{
  var p, Grid;
  // Obtain the grid object
  p = Sys.Process("EmployeeTerritoryOrder");
  p.WinFormsObject("Form1").Maximize();
  Grid = p.WinFormsObject("Form1").WinFormsObject("gridGroupingControl1");

  // Sort data in the root table
  Grid.ClickColumnHeader("LastName");

  // Sort data in a nested table
  Grid.ExpandChildView(2, 1);
  Grid.wChildView(2, 1).DblClickColumnHeader("OrderDate");
}

Python

def Main ():
  # Obtain the grid object
  p = Sys.Process("EmployeeTerritoryOrder")
  p.WinFormsObject("Form1").Maximize()
  Grid = p.WinFormsObject("Form1").WinFormsObject("gridGroupingControl1")

  # Sort data in the root table
  Grid.ClickColumnHeader("LastName")

  # Sort data in a nested table
  Grid.ExpandChildView(2, 1)
  Grid.wChildView[2, 1].DblClickColumnHeader("OrderDate")

VBScript

Sub Main
  Dim p, Grid

  ' Obtain the grid object
  Set p = Sys.Process("EmployeeTerritoryOrder")
  p.WinFormsObject("Form1").Maximize
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("gridGroupingControl1")

  ' Sort data in the root table
  Grid.ClickColumnHeader("LastName")

  ' Sort data in a nested table
  Call Grid.ExpandChildView(2, 1)
  Grid.wChildView(2, 1).DblClickColumnHeader ("OrderDate")
End Sub

DelphiScript

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

  // Sort data in the root table
  Grid.ClickColumnHeader('LastName');

  // Sort data in a nested table
  Grid.ExpandChildView(2, 1);
  Grid.wChildView(2, 1).DblClickColumnHeader('OrderDate');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;
  // Obtain the grid object
  p = Sys["Process"]("EmployeeTerritoryOrder");
  p.WinFormsObject("Form1").Maximize();
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("gridGroupingControl1");

  // Sort data in the root table
  Grid["ClickColumnHeader"]("LastName");

  // Sort data in a nested table
  Grid["ExpandChildView"](2, 1);
  Grid["wChildView"](2, 1)["DblClickColumnHeader"]("OrderDate");
}

Using GridGroupingControl Internal Members

To sort data displayed in the GridGroupingControl, you can also use internal methods of the TableDescriptor.SortedColumns collection of a grid table object:

  • Add_2 (FieldName) sorts the grid data by the specified column in the ascending direction, preserving the previous sort conditions. The FieldName parameter specifies the column’s bound dataset field name.
  • Add_3 (FieldName, SortDirection) is an overload variant of the Add method. It sorts the grid data by the specified column preserving the previous sort conditions. The FieldName parameter specifies the column’s bound dataset field name. The SortDirection parameter specifies the sort direction and can have the following string values: “Ascending” or “Descending”.
  • Clear() removes sorting by all columns.

The example below demonstrates how you can use these methods to sort data in GridGroupingControl.

Example

View description

JavaScript

function Main ()
{
  var p, Grid, Table;

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

  // Clear sorting in the root table
  ClearSort (Grid, null);
  // Sort data in the root table
  Sort (Grid, null, "LastName", "Ascending");

  Table = GetNestedTable (Grid, null, 2, "Orders");
  // Clear sorting in a nested table
  ClearSort (Grid, Table);
  // Sort data in a nested table
  Sort (Grid, Table, "OrderDate", "Descending");
}

function Sort (Grid, Table, ColumnId, SortDirection)
{
  // Get the root table if no table is specified
  if (strictEqual(Table, null))
    Table = Grid.Table.TopLevelGroup;

  // Get the column object
  let Column = GetColumn (Grid, Table, ColumnId);

  // Sort data by this column
  Table.ParentTableDescriptor.SortedColumns.Add_3 (Column.MappingName, SortDirection);
}

function ClearSort (Grid, Table)
{
  if (strictEqual(Table, null))
    Grid.TableDescriptor.SortedColumns.Clear()
  else
    Table.ParentTableDescriptor.SortedColumns.Clear();
}

function GetNestedTable (Grid, TableOrGroup, RowIndex, TableId)
{
  // Get the root table if no table is specified
  if (strictEqual(TableOrGroup, null))
    TableOrGroup = Grid.Table.TopLevelGroup;

  // Get the nested tables collection
  let NestedTables = TableOrGroup.Records.Item_2(RowIndex).NestedTables;

  // Check if the table is specified by caption or index
  if (equal(aqObject.GetVarType (TableId), varOleStr))
    return NestedTables.Item_2(TableId).ChildTable
  else
    return NestedTables.Item(TableId).ChildTable;
}

function GetColumn (Grid, Table, ColumnId)
{
  var Columns, ColDescriptor;

  // Get the table columns
  if (strictEqual(Table, null))
    Columns = Grid.TableDescriptor.Columns
  else
    Columns = Table.ParentTableDescriptor.Columns;

  if (equal(aqObject.GetVarType (ColumnId), varOleStr))
  // The column is specified by its caption
  {
    // Search for the desired column
    for (let i=0; i<Columns.Count; i++)
    {
      ColDescriptor = Columns.Item(i);
      if (equal(ColDescriptor.HeaderText.OleValue, ColumnId))
        return ColDescriptor; // Column is found
    }
    return null; // Column is not found
  }
  else
    // The column is specified by its index
    return Columns.Item(ColumnId);
}

JScript

function Main ()
{
  var p, Grid, Table;

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

  // Clear sorting in the root table
  ClearSort (Grid, null);
  // Sort data in the root table
  Sort (Grid, null, "LastName", "Ascending");

  Table = GetNestedTable (Grid, null, 2, "Orders");
  // Clear sorting in a nested table
  ClearSort (Grid, Table);
  // Sort data in a nested table
  Sort (Grid, Table, "OrderDate", "Descending");
}

function Sort (Grid, Table, ColumnId, SortDirection)
{
  // Get the root table if no table is specified
  if (Table == null)
    Table = Grid.Table.TopLevelGroup;

  // Get the column object
  var Column = GetColumn (Grid, Table, ColumnId);

  // Sort data by this column
  Table.ParentTableDescriptor.SortedColumns.Add_3 (Column.MappingName, SortDirection);
}

function ClearSort (Grid, Table)
{
  if (Table == null)
    Grid.TableDescriptor.SortedColumns.Clear()
  else
    Table.ParentTableDescriptor.SortedColumns.Clear();
}

function GetNestedTable (Grid, TableOrGroup, RowIndex, TableId)
{
  // Get the root table if no table is specified
  if (TableOrGroup == null)
    TableOrGroup = Grid.Table.TopLevelGroup;

  // Get the nested tables collection
  var NestedTables = TableOrGroup.Records.Item_2(RowIndex).NestedTables;

  // Check if the table is specified by caption or index
  if (aqObject.GetVarType (TableId) == varOleStr)
    return NestedTables.Item_2(TableId).ChildTable
  else
    return NestedTables.Item(TableId).ChildTable;
}

function GetColumn (Grid, Table, ColumnId)
{
  var Columns, ColDescriptor, i;

  // Get the table columns
  if (Table == null)
    Columns = Grid.TableDescriptor.Columns
  else
    Columns = Table.ParentTableDescriptor.Columns;

  if (aqObject.GetVarType (ColumnId) == varOleStr)
  // The column is specified by its caption
  {
    // Search for the desired column
    for (i=0; i<Columns.Count; i++)
    {
      ColDescriptor = Columns.Item(i);
      if (ColDescriptor.HeaderText.OleValue == ColumnId)
        return ColDescriptor; // Column is found
    }
    return null; // Column is not found
  }
  else
    // The column is specified by its index
    return Columns.Item(ColumnId);
}

Python

def Main ():

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

  # Clear sorting in the root table
  ClearSort (Grid, None)
  # Sort data in the root table
  Sort (Grid, None, "LastName", "Ascending")

  Table = GetNestedTable (Grid, None, 2, "Orders")
  # Clear sorting in a nested table
  ClearSort (Grid, Table)
  # Sort data in a nested table
  Sort (Grid, Table, "OrderDate", "Descending")

def Sort (Grid, Table, ColumnId, SortDirection):
  # Get the root table if no table is specified
  if (Table == None):
    Table = Grid.Table.TopLevelGroup

  # Get the column object
  Column = GetColumn (Grid, Table, ColumnId)

  # Sort data by this column
  Table.ParentTableDescriptor.SortedColumns.Add_3 (Column.MappingName, SortDirection)

def ClearSort (Grid, Table):
  if (Table == None):
    Grid.TableDescriptor.SortedColumns.Clear()
  else:
    Table.ParentTableDescriptor.SortedColumns.Clear()

def GetNestedTable (Grid, TableOrGroup, RowIndex, TableId):
  # Get the root table if no table is specified
  if (TableOrGroup == None):
    TableOrGroup = Grid.Table.TopLevelGroup

  # Get the nested tables collection
  NestedTables = TableOrGroup.Records.Item_2(RowIndex).NestedTables

  # Check if the table is specified by caption or index
  if (aqObject.GetVarType (TableId) == varOleStr):
    return NestedTables.Item_2[TableId].ChildTable
  else:
    return NestedTables.Item[TableId].ChildTable

def GetColumn (Grid, Table, ColumnId):

  # Get the table columns
  if (Table == None):
    Columns = Grid.TableDescriptor.Columns
  else:
    Columns = Table.ParentTableDescriptor.Columns

  if (aqObject.GetVarType (ColumnId) == varOleStr):
  # The column is specified by its caption
    # Search for the desired column
    for i in range(0, Columns.Count-1):
      ColDescriptor = Columns.Item[i]
      if (ColDescriptor.HeaderText.OleValue == ColumnId):
        return ColDescriptor # Column is found
    return None # Column is not found
  else:
    # The column is specified by its index
    return Columns.Item[ColumnId]

VBScript

Sub Main
  Dim p, Grid, Table

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

  ' Clear sorting in the root table
  Call ClearSort (Grid, Nothing)
  ' Sort data in the root table
  Call Sort (Grid, Nothing, "LastName", "Ascending")

  Set Table = GetNestedTable (Grid, Nothing, 2, "Orders")
  ' Clear sorting in a nested table
  Call ClearSort (Grid, Table)
  ' Sort data in a nested table
  Call Sort (Grid, Table, "OrderDate", "Descending")
End Sub

Sub Sort (Grid, Table, ColumnId, SortDirection)
  Dim Column

  ' Get the root table if no table is specified
  If Table Is Nothing Then
    Set Table = Grid.Table.TopLevelGroup
  End If

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

  ' Sort data by this column
  Call Table.ParentTableDescriptor.SortedColumns.Add_3 (Column.MappingName, SortDirection)
End Sub

Sub ClearSort (Grid, Table)
  If Table Is Nothing Then
    Grid.TableDescriptor.SortedColumns.Clear
  Else
    Table.ParentTableDescriptor.SortedColumns.Clear
  End If
End Sub

Function GetNestedTable (Grid, Table, RowIndex, TableId)
  Dim NestedTables

  ' Get the root table if no table is specified
  If Table Is Nothing Then
    Set Table = Grid.Table.TopLevelGroup
  End If

  ' Get the nested tables collection
  Set NestedTables = Table.Records.Item_2(RowIndex).NestedTables

  ' Check if the table is specified by caption or index
  If aqObject.GetVarType (TableId) = varOleStr Then
    Set GetNestedTable = NestedTables.Item_2(TableId).ChildTable
  Else
    Set GetNestedTable = NestedTables.Item(TableId).ChildTable
  End If
End Function

Function GetColumn (Grid, Table, ColumnId)
  Dim Columns, ColDescriptor, i

  ' Get the table columns
  If Table Is Nothing Then
    Set Columns = Grid.TableDescriptor.Columns
  Else
    Set Columns = Table.ParentTableDescriptor.Columns
  End If

  If aqObject.GetVarType (ColumnId) = varOleStr Then
    ' The column is specified by its caption
    ' Search for the desired column
    For i=0 To Columns.Count-1
      Set ColDescriptor = Columns.Item(i)
      If ColDescriptor.HeaderText.OleValue = ColumnId Then
        Set GetColumn = ColDescriptor ' Column is found
        Exit Function
      End If
    Next
    Set GetColumn = Nothing ' Column is not found
  Else
    ' The column is specified by its index
    Set GetColumn = Columns.Item(ColumnId)
  End If
End Function

DelphiScript

procedure Sort (Grid, Table, ColumnId, SortDirection); forward;
procedure ClearSort (Grid, Table); forward;
function GetNestedTable (Grid, TableOrGroup, RowIndex, TableId); forward;
function GetColumn (Grid, Table, ColumnId); forward;

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

  // Clear sorting in the root table
  ClearSort (Grid, nil);
  // Sort data in the root table
  Sort (Grid, nil, 'LastName', 'Ascending');

  Table := GetNestedTable (Grid, nil, 2, 'Orders');
  // Clear sorting in a nested table
  ClearSort (Grid, Table);
  // Sort data in a nested table
  Sort (Grid, Table, 'OrderDate', 'Descending');
end;

procedure Sort (Grid, Table, ColumnId, SortDirection);
var Column : OleVariant;
begin
  // Get the root table if no table is specified
  if Table = nil then
    Table := Grid.Table.TopLevelGroup;

  // Get the column object
  Column := GetColumn (Grid, Table, ColumnId);

  // Sort data by this column
  Table.ParentTableDescriptor.SortedColumns.Add_3 (Column.MappingName, SortDirection);
end;

procedure ClearSort (Grid, Table);
begin
  if Table = nil then
    Grid.TableDescriptor.SortedColumns.Clear
  else
    Table.ParentTableDescriptor.SortedColumns.Clear;
end;

function GetNestedTable (Grid, TableOrGroup, RowIndex, TableId);
var NestedTables : OleVariant;
begin
  // Get the root table if no table is specified
  if TableOrGroup = nil then
    TableOrGroup := Grid.Table.TopLevelGroup;

  // Get the nested tables collection
  NestedTables := TableOrGroup.Records.Item_2[RowIndex].NestedTables;

  // Check if the table is specified by caption or index
  if aqObject.GetVarType (TableId) = varOleStr then
    Result := NestedTables.Item_2[TableId].ChildTable
  else
    Result := NestedTables.Item[TableId].ChildTable;
end;

function GetColumn (Grid, Table, ColumnId);
var Columns, ColDescriptor, i : OleVariant;
begin
  // Get the table columns
  if Table = nil then
    Columns := Grid.TableDescriptor.Columns
  else
    Columns := Table.ParentTableDescriptor.Columns;

  if aqObject.GetVarType (ColumnId) = varOleStr then
  // The column is specified by its caption
  begin
    // Search for the desired column
    for i:=0 to Columns.Count-1 do
    begin
      ColDescriptor := Columns.Item[i];
      if ColDescriptor.HeaderText.OleValue = ColumnId then
      begin
        Result := ColDescriptor; // Column is found
        Exit;
      end
    end;
    Result := nil; // Column is not found
  end
  else
    // The column is specified by its index
    Result := Columns.Item[ColumnId];
end;

C++Script, C#Script

function Main ()
{
  var p, Grid, Table;

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

  // Clear sorting in the root table
  ClearSort (Grid, null);
  // Sort data in the root table
  Sort (Grid, null, "LastName", "Ascending");

  Table = GetNestedTable (Grid, null, 2, "Orders");
  // Clear sorting in a nested table
  ClearSort (Grid, Table);
  // Sort data in a nested table
  Sort (Grid, Table, "OrderDate", "Descending");
}

function Sort (Grid, Table, ColumnId, SortDirection)
{
  // Get the root table if no table is specified
  if (Table == null)
    Table = Grid["Table"]["TopLevelGroup"];

  // Get the column object
  var Column = GetColumn (Grid, Table, ColumnId);

  // Sort data by this column
  Table["ParentTableDescriptor"]["SortedColumns"]["Add_3"](Column["MappingName"], SortDirection);
}

function ClearSort (Grid, Table)
{
  if (Table == null)
    Grid["TableDescriptor"]["SortedColumns"]["Clear"]()
  else
    Table["ParentTableDescriptor"]["SortedColumns"]["Clear"]();
}

function GetNestedTable (Grid, TableOrGroup, RowIndex, TableId)
{
  // Get the root table if no table is specified
  if (TableOrGroup == null)
    TableOrGroup = Grid["Table"]["TopLevelGroup"];

  // Get the nested tables collection
  var NestedTables = TableOrGroup["Records"]["Item_2"](RowIndex)["NestedTables"];

  // Check if the table is specified by caption or index
  if (aqObject["GetVarType"] (TableId) == varOleStr)
    return NestedTables["Item_2"](TableId)["ChildTable"]
  else
    return NestedTables["Item"](TableId)["ChildTable"];
}

function GetColumn (Grid, Table, ColumnId)
{
  var Columns, ColDescriptor, i;

  // Get the table columns
  if (Table == null)
    Columns = Grid["TableDescriptor"]["Columns"]
  else
    Columns = Table["ParentTableDescriptor"]["Columns"];

  if (aqObject["GetVarType"] (ColumnId) == varOleStr)
  // The column is specified by its caption
  {
    // Search for the desired column
    for (i=0; i<Columns["Count"]; i++)
    {
      ColDescriptor = Columns["Item"](i);
      if (ColDescriptor["HeaderText"]["OleValue"] == ColumnId)
        return ColDescriptor; // Column is found
    }
    return null; // Column is not found
  }
  else
    // The column is specified by its index
    return Columns["Item"](ColumnId);
}

See Also

Working With Syncfusion GridGroupingControl
ClickColumnHeader Action (Grid Controls)

Highlight search results