Sorting Data in Infragistics UltraGrid

Applies to TestComplete 15.46, last modified on January 09, 2023

The data displayed by the UltraGrid 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:

To perform these actions, TestComplete should have access to internal objects, properties and methods of the UltraGrid control. For this purpose, the .NET Application Support plugin must be installed and enabled.

When testing Infragistics UltraGrid controls, use specific methods and properties of the corresponding Infragistics UltraGrid 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 UltraGrid 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 should hold the Shift key while clicking the column header.

You can simulate clicks over the UltraGrid column headers using the ClickColumnHeader action either of InfragisticsUltraGrid or InfragisticsUltraGridView objects. The action of the InfragisticsUltraGrid object applies to headers of the main data view, whereas the InfragisticsUltraGridView action affect the headers of the respective child data view. Note that it is only possible to simulate clicks on column headers if they are displayed, that is, if the view’s UltraGridBandObj.ColHeadersVisible property is True. If this property is False, the column headers are hidden, so an attempt to simulate a click on them will cause an error.

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

Note: This example works with the SamplesExplorer application that is shipped with the Infragistics NetAdvantage Suite.

JavaScript, JScript

function ColumnClickSorting()
{
  var p, Grid
  // Obtain the application process
  p = Sys.Process("SamplesExplorer");
  // Select the "Merged Cells Feature" demo
  p.frmMain.WinFormsObject("tvwSamples").DblClickItem("|Feature Samples|V5.1 Merged Cells Feature");
  // Obtain the grid control
  Grid = p.frmMergedCells.WinFormsObject("UltraGrid1");
  
  // Sort the root table data
  Grid.ClickColumnHeader("Country");
  Grid.ClickColumnHeader("City", skShift);
  // Display the child table of the first row
  Grid.Expand(0);
  // Sort the child table data
  Grid.wChildView(0).ClickColumnHeader("CustomerID");
}

Python

def ColumnClickSorting():
  # Obtain the application process
  p = Sys.Process("SamplesExplorer")
  # Select the "Merged Cells Feature" demo
  p.frmMain.WinFormsObject("tvwSamples").DblClickItem("|Feature Samples|V5.1 Merged Cells Feature")
  # Obtain the grid control
  Grid = p.frmMergedCells.WinFormsObject("UltraGrid1")
  
  # Sort the root table data
  Grid.ClickColumnHeader("Country")
  Grid.ClickColumnHeader("City", skShift)
  # Display the child table of the first row 
  Grid.Expand(0)
  # Sort the child table data
  Grid.wChildView[0].ClickColumnHeader("CustomerID")

VBScript

Sub ColumnClickSorting
  Dim p
  Dim Grid
  ' Obtain the application process
  Set p = Sys.Process("SamplesExplorer")
  ' Select the "Merged Cells Feature" demo
  Call p.frmMain.WinFormsObject("tvwSamples").DblClickItem("|Feature Samples|V5.1 Merged Cells Feature")
  ' Obtain the grid control
  Set Grid = p.frmMergedCells.WinFormsObject("UltraGrid1")
  
  ' Sort the root table data
  Call Grid.ClickColumnHeader("Country")
  Call Grid.ClickColumnHeader("City", skShift)
  ' Display the child table of the first row
  Call Grid.Expand(0)
  ' Sort the child table data
  Call Grid.wChildView(0).ClickColumnHeader("CustomerID")
End Sub

DelphiScript

procedure ColumnClickSorting;
  var p, Grid: OleVariant;
begin
  // Obtain the application process
  p := Sys.Process('SamplesExplorer');
  // Select the "Merged Cells Feature" demo
  p.frmMain.WinFormsObject('tvwSamples').DblClickItem('|Feature Samples|V5.1 Merged Cells Feature');
  // Obtain the grid control
  Grid := p.frmMergedCells.WinFormsObject('UltraGrid1');
  
  // Sort the root table data
  Grid.ClickColumnHeader('Country');
  Grid.ClickColumnHeader('City', skShift);
  // Display the child table of the first row
  Grid.Expand(0);
  // Sort the child table data
  Grid.wChildView[0].ClickColumnHeader('CustomerID');
end;

C++Script, C#Script

function ColumnClickSorting()
{
  var p, Grid
  // Obtain the application process
  p = Sys["Process"]("SamplesExplorer");
  // Select the "Merged Cells Feature" demo
  p["frmMain"]["WinFormsObject"]("tvwSamples")["DblClickItem"]("|Feature Samples|V5.1 Merged Cells Feature");
  // Obtain the grid control
  Grid = p["frmMergedCells"]["WinFormsObject"]("UltraGrid1");
  
  // Sort the root table data
  Grid["ClickColumnHeader"]("Country");
  Grid["ClickColumnHeader"]("City", skShift);
  // Display the child table of the first row
  Grid["Expand"](0);
  // Sort the child table data
  Grid["wChildView"](0)["ClickColumnHeader"]("CustomerID");
}

Using UltraGrid Internal Members

The drawback of the previous approach is that the initial sort order of a column is unknown. To determine the sort order you should read the SortIndicator property of the internal UltraGridColumn object.

To obtain the UltraGridColumn object that corresponds to the desired column, you can use the GetColumn routine whose code is given in the Accessing Grid Elements in Infragistics UltraGrid topic. It retrieves a column specified by the caption or by the visible position in a grid.

The SortIndicator property can accept the following string values:

  • Ascending - Denotes that a column is sorted ascending.
  • Descending - Senotes that a column is sorted descending.
  • None - Denotes that a column is not sorted.
  • Disabled - Denotes that the column cannot be sorted.

Note that assigning the column’s SortIndicator property does not automatically cancel sorting by other column(s).

Example

View description

JavaScript

function Main()
{
var p, Grid;
  // Obtain the grid object
  p = Sys.Process("SamplesExplorer");
  Grid = p.WinFormsObject("frmSort").WinFormsObject("UltraGrid1");
  
  // Manipulate column sorting
  ColumnSortAscending(Grid, null, "Name");
  ColumnSortDisabled(Grid, null, "Name");
  ColumnSortDescending(Grid, null, "Shuttle_ID");
  ColumnSortReset(Grid, null, "Name");
}

function ColumnSortAscending(grid, childBand, columnId)
{
    // Obtain the row object
  let Column = GetColumn(grid, childBand, columnId)
  
  if (!strictEqual(Column, null))
    // Set the desired sorting state
    Column.SortIndicator = "Ascending";
}

function ColumnSortDescending(grid, childBand, columnId)
{
    // Obtain the row object
  let Column = GetColumn(grid, childBand, columnId)
  
  if (!strictEqual(Column, null))
    // Set the desired sorting state
    Column.SortIndicator = "Descending";
}

function ColumnSortReset(grid, childBand, columnId)
{
    // Obtain the row object
  let Column = GetColumn(grid, childBand, columnId)
  
  if (!strictEqual(Column, null))
    // Set the desired sorting state
    Column.SortIndicator = "None";
}

function ColumnSortDisabled(grid, childBand, columnId)
{
    // Obtain the row object
  let Column = GetColumn(grid, childBand, columnId)
  
  if (!strictEqual(Column, null))
    // Set the desired sorting state
    Column.SortIndicator = "Disabled";
}

JScript

function Main()
{
var p, Grid;
  // Obtain the grid object
  p = Sys.Process("SamplesExplorer");
  Grid = p.WinFormsObject("frmSort").WinFormsObject("UltraGrid1");
  
  // Manipulate column sorting
  ColumnSortAscending(Grid, null, "Name");
  ColumnSortDisabled(Grid, null, "Name");
  ColumnSortDescending(Grid, null, "Shuttle_ID");
  ColumnSortReset(Grid, null, "Name");
}

function ColumnSortAscending(grid, childBand, columnId)
{
    // Obtain the row object
  var Column = GetColumn(grid, childBand, columnId)
  
  if (Column != null)
    // Set the desired sorting state
    Column.SortIndicator = "Ascending";
}

function ColumnSortDescending(grid, childBand, columnId)
{
    // Obtain the row object
  var Column = GetColumn(grid, childBand, columnId)
  
  if (Column != null)
    // Set the desired sorting state
    Column.SortIndicator = "Descending";
}

function ColumnSortReset(grid, childBand, columnId)
{
    // Obtain the row object
  var Column = GetColumn(grid, childBand, columnId)
  
  if (Column != null)
    // Set the desired sorting state
    Column.SortIndicator = "None";
}

function ColumnSortDisabled(grid, childBand, columnId)
{
    // Obtain the row object
  var Column = GetColumn(grid, childBand, columnId)
  
  if (Column != null)
    // Set the desired sorting state
    Column.SortIndicator = "Disabled";
}

Python

def Main():
  # Obtain the grid object
  p = Sys.Process("SamplesExplorer")
  Grid = p.WinFormsObject("frmSort").WinFormsObject("UltraGrid1")
  
  # Manipulate column sorting
  ColumnSortAscending(Grid, None, "Name")
  ColumnSortDisabled(Grid, None, "Name")
  ColumnSortDescending(Grid, None, "Shuttle_ID")
  ColumnSortReset(Grid, None, "Name")

def ColumnSortAscending(grid, childBand, columnId):
  # Obtain the row object
  Column = GetColumn(grid, childBand, columnId)
  
  if (Column != None):
    # Set the desired sorting state
    Column.SortIndicator = "Ascending"

def ColumnSortDescending(grid, childBand, columnId):
  # Obtain the row object
  Column = GetColumn(grid, childBand, columnId)
  
  if (Column != None):
    # Set the desired sorting state
    Column.SortIndicator = "Descending"

def ColumnSortReset(grid, childBand, columnId):
  # Obtain the row object
  Column = GetColumn(grid, childBand, columnId)
  
  if (Column != None):
    # Set the desired sorting state
    Column.SortIndicator = "None"

def ColumnSortDisabled(grid, childBand, columnId):
  # Obtain the row object
  Column = GetColumn(grid, childBand, columnId)
  
  if (Column != None):
    # Set the desired sorting state
    Column.SortIndicator = "Disabled"

VBScript

Sub Main
  ' Obtain the grid object
  Set p = Sys.Process("SamplesExplorer")
  Set Grid = p.WinFormsObject("frmSort").WinFormsObject("UltraGrid1")
  
  ' Manipulate column sorting
  Call ColumnSortAscending(Grid, Nothing, "Name")
  Call ColumnSortDisabled(Grid, Nothing, "Name")
  Call ColumnSortDescending(Grid, Nothing, "Shuttle_ID")
  Call ColumnSortReset(Grid, Nothing, "Name")
End Sub

Sub ColumnSortAscending(grid, childBand, columnId)
  ' Obtain the specified column
  Set Column = GetColumn(grid, childBand, columnId)
  If Not Column Is Nothing Then
      ' Set the desired sorting state
      Column.SortIndicator = "Ascending"
  End If
End Sub

Sub ColumnSortDescending(grid, childBand, columnId)
  ' Obtain the specified column
  Set Column = GetColumn(grid, childBand, columnId)
  If Not Column Is Nothing Then
      ' Set the desired sorting state
      Column.SortIndicator = "Descending"
  End If
End Sub

Sub ColumnSortReset(grid, childBand, columnId)
  ' Obtain the specified column
  Set Column = GetColumn(grid, childBand, columnId)
  If Not Column Is Nothing Then
      ' Set the desired sorting state
      Column.SortIndicator = "None"
  End If
End Sub

Sub ColumnSortDisabled(grid, childBand, columnId)
  ' Obtain the specified column
  Set Column = GetColumn(grid, childBand, columnId)
  If Not Column Is Nothing Then
      ' Set the desired sorting state
      Column.SortIndicator = "Disabled"
  End If
End Sub

DelphiScript

procedure ColumnSortAscending(grid, childBand, columnId); forward;
procedure ColumnSortDescending(grid, childBand, columnId); forward;
procedure ColumnSortReset(grid, childBand, columnId); forward;
procedure ColumnSortDisabled(grid, childBand, columnId); forward;

procedure Main;
var p, Grid: Variant;
begin
  // Obtain the grid object
  p := Sys.Process('SamplesExplorer');
  Grid := p.WinFormsObject('frmSort').WinFormsObject('UltraGrid1');
  
  // Manipulate column sorting
  ColumnSortAscending(Grid, nil, 'Name');
  ColumnSortDisabled(Grid, nil, 'Name');
  ColumnSortDescending(Grid, nil, 'Shuttle_ID');
  ColumnSortReset(Grid, nil, 'Name');
end;

procedure ColumnSortAscending(grid, childBand, columnId);
var
  Column: Variant;
begin
  // Obtain the specified column
  Column := GetColumn(grid, childBand, columnId);
  if Column <> nil then
      // Set the desired sorting state
      Column.SortIndicator := 'Ascending';
end;

procedure ColumnSortDescending(grid, childBand, columnId);
var
  Column: Variant;
begin
  // Obtain the specified column
  Column := GetColumn(grid, childBand, columnId);
  if Column <> nil then
      // Set the desired sorting state
      Column.SortIndicator := 'Descending';
end;


procedure ColumnSortReset(grid, childBand, columnId);
var
  Column: Variant;
begin
  // Obtain the specified column
  Column := GetColumn(grid, childBand, columnId);
  if Column <> nil then
     // Set the desired sorting state
    Column.SortIndicator := 'None';
end;

procedure ColumnSortDisabled(grid, childBand, columnId);
var
  Column: Variant;
begin
  // Obtain the specified column
  Column := GetColumn(grid, childBand, columnId);
  if Column <> nil then
      // Set the desired sorting state
      Column.SortIndicator := 'Disabled';
end;

C++Script, C#Script

function Main()
{
var p, Grid;
  // Obtain the grid object
  p = Sys["Process"]("SamplesExplorer");
  Grid = p["WinFormsObject"]("frmSort")["WinFormsObject"]("UltraGrid1");
  
  // Manipulate column sorting
  ColumnSortAscending(Grid, null, "Name");
  ColumnSortDisabled(Grid, null, "Name");
  ColumnSortDescending(Grid, null, "Shuttle_ID");
  ColumnSortReset(Grid, null, "Name");
}

function ColumnSortAscending(grid, childBand, columnId)
{
    // Obtain the row object
  var Column = GetColumn(grid, childBand, columnId)
  
  if (Column != null)
    // Set the desired sorting state
    Column["SortIndicator"] = "Ascending";
}

function ColumnSortDescending(grid, childBand, columnId)
{
    // Obtain the row object
  var Column = GetColumn(grid, childBand, columnId)
  
  if (Column != null)
    // Set the desired sorting state
    Column["SortIndicator"] = "Descending";
}

function ColumnSortReset(grid, childBand, columnId)
{
    // Obtain the row object
  var Column = GetColumn(grid, childBand, columnId)
  
  if (Column != null)
    // Set the desired sorting state
    Column["SortIndicator"] = "None";
}

function ColumnSortDisabled(grid, childBand, columnId)
{
    // Obtain the row object
  var Column = GetColumn(grid, childBand, columnId)
  
  if (Column != null)
    // Set the desired sorting state
    Column["SortIndicator"] = "Disabled";
}

See Also

Working With Infragistics UltraGrid
Accessing Grid Elements in Infragistics UltraGrid
ClickColumnHeader Action (Grid Controls)

Highlight search results