Sorting Data in Microsoft DataGrid

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

The Microsoft DataGrid .NET control lets the end users sort the displayed data by clicking on the header of the column that they want to sort. The next click on the same header switches the sort direction (from ascending to descending, and vice versa). Note, that this functionality is only enabled if the grid’s AllowSorting property is True. Otherwise, if this property is False, the DataGrid control cannot be sorted.

With TestComplete, you can simulate clicks on the grid column headers using the ClickColumnHeader and DblClickColumnHeader actions of the MicrosoftDataGrid object. Their parameters specify the column to be clicked and also whether the Shift, Ctrl, Alt or combination of these keys should be pressed during the click. The shift keys may be useful, for example, if the grid control in the tested application can be sorted by multiple columns, depending on the shift keys combination.

Note: The ClickColummHeader and DblClickColumnHeader actions, as well as other methods, properties and actions of the MicrosoftDataGrid object are only available if the Microsoft Control Support plugin is installed and enabled.

The following example demonstrates how to use the mentioned action for sorting data displayed in the DataGrid control:

JavaScript, JScript

function Main ()
{
  var p, Grid;

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

  // Click the column header once to sort ascending by that column
  Grid.ClickColumnHeader ("Customer Name");

  aqUtils.Delay (1000);

  // Double-click the column header to sort descending by that column
  Grid.DblClickColumnHeader ("Product");
}

Python

def Main ():

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

  # Click the column header once to sort ascending by that column
  Grid.ClickColumnHeader ("Customer Name")

  aqUtils.Delay (1000)

  # Double-click the column header to sort descending by that column
  Grid.DblClickColumnHeader ("Product")

VBScript

Sub Main
  Dim p, Grid

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

  ' Click the column header once to sort ascending by that column
  Grid.ClickColumnHeader "Customer Name"

  aqUtils.Delay 1000

  ' Double-click the column header to sort descending by that column
  Grid.ClickColumnHeader "Product"
End Sub

DelphiScript

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

  // Click the column header once to sort ascending by that column
  Grid.ClickColumnHeader ('Customer Name');

  aqUtils.Delay (1000);

  // Double-click the column header to sort descending by that column
  Grid.DblClickColumnHeader ('Product');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

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

  // Click the column header once to sort ascending by that column
  Grid["ClickColumnHeader"]("Customer Name");

  aqUtils["Delay"] (1000);

  // Double-click the column header to sort descending by that column
  Grid["DblClickColumnHeader"]("Product");
}

See Also

Working With Microsoft DataGrid
ClickColumnHeader Action (Grid Controls)
DblClickColumnHeader Action (Grid Controls)

Highlight search results