Sorting Data in Syncfusion GridControl

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

The data displayed by Syncfusion GridControl can be sorted by a particular column. Typically, users can sort the grid data and change the sort direction (ascending or descending) by clicking or double-clicking the column header. However, implementing this functionality in scripts is a difficult task and we will not consider it here. Below, we will explain how to perform sorting using internal methods of the GridControl object.

To sort data displayed in a GridControl, you can use various overloaded versions of the grid’s GridData.SortByColumn method:

  • SortByColumn (ColIndex) - sorts the grid data ascending.
  • SortByColumn_2 (ColIndexDirection) - sorts the grid data in the specified direction.
  • SortByColumn_4 (ColIndexDirectionGridObj.Model.Rows.HeaderCount) - sorts the grid data in the specified direction.

These methods take the column index and the string specifying the sort direction (“Ascending” or “Descending”) as parameters. The SortByColumn_4 method is useful when column headers occupy more than one row, since it excludes additional headers from sorting. Note that sorting a GridControl data by a particular column automatically cancels sorting by other columns.

Note: In order for TestComplete to access the mentioned properties and methods of the GridControl object, the .NET Application Support plugin must be installed and enabled.

Below is an example that illustrates how you can sort data displayed in the GridControl.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid;

  // Obtain the application process and the grid object
  p = Sys.Process("GridControlSort");
  Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1");

  // Sort data by the "Date" column
  Sort (Grid, 2, "Ascending");

  aqUtils.Delay (1000);

  // Sort data by the "Text" column
  Sort (Grid, 5, "Descending");
}

function Sort (Grid, ColIndex, SortOrder)
{
  Grid.Data.SortByColumn_4 (ColIndex, SortOrder, Grid.Model.Rows.HeaderCount);
}

Python

def Main ():

  # Obtain the application process and the grid object
  p = Sys.Process("GridControlSort")
  Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1")

  # Sort data by the "Date" column
  Sort (Grid, 2, "Ascending")

  aqUtils.Delay (1000)

  # Sort data by the "Text" column
  Sort (Grid, 5, "Descending")

def Sort (Grid, ColIndex, SortOrder):
  Grid.Data.SortByColumn_4 (ColIndex, SortOrder, Grid.Model.Rows.HeaderCount)

VBScript

Sub Main
  Dim p, Grid
      
  ' Obtain the application process and the grid object
  Set p = Sys.Process("GridControlSort")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("gridControl1")

  ' Sort data by the "Date" column
  Call Sort (Grid, 2, "Ascending")

  Call aqUtils.Delay (1000)

  ' Sort data by the "Text" column
  Call Sort (Grid, 5, "Descending")
End Sub

Sub Sort (Grid, ColIndex, SortOrder)
  Call Grid.Data.SortByColumn_4 (ColIndex, SortOrder, Grid.Model.Rows.HeaderCount)
End Sub

DelphiScript

procedure Sort (Grid, ColIndex, SortOrder);
begin
  Grid.Data.SortByColumn_4 (ColIndex, SortOrder, Grid.Model.Rows.HeaderCount);
end;

procedure Main;
var p, Grid : OleVariant;
begin
  // Obtain the application process and the grid object
  p := Sys.Process('GridControlSort');
  Grid := p.WinFormsObject('Form1').WinFormsObject('gridControl1');

  // Sort data by the "Date" column
  Sort (Grid, 2, 'Ascending');

  aqUtils.Delay (1000);

  // Sort data by the "Text" column
  Sort (Grid, 5, 'Descending');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

  // Obtain the application process and the grid object
  p = Sys["Process"]("GridControlSort");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("gridControl1");

  // Sort data by the "Date" column
  Sort (Grid, 2, "Ascending");

  aqUtils["Delay"] (1000);

  // Sort data by the "Text" column
  Sort (Grid, 5, "Descending");
}

function Sort (Grid, ColIndex, SortOrder)
{
  Grid["Data"]["SortByColumn_4"](ColIndex, SortOrder, Grid["Model"]["Rows"]["HeaderCount"]);
}

See Also

Working With Syncfusion GridControl
Accessing Rows, Columns and Cells in Syncfusion GridControl

Highlight search results