Sorting Data in Developer Express XtraGrid

The Developer Express XtraGrid control lets the end users sort the
displayed data. So you may need to sort the XtraGrid data from your
TestComplete scripts. In this article, I will describe two approaches
that can be used to sort the XtraGrid data - simulating column header
clicks and using internal members of the XtraGrid control.

Both approaches require access to internal properties, methods and objects of the XtraGrid control. To enable TestComplete to access them, you need to install and enable the .NET Open Application Support plug-in.

Simulating Clicks on Column Headers

It is possible to sort the data displayed in the XtraGrid control 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 Shift key while clicking the column header. Holding Ctrl while clicking the header removes sorting by this column, preserving sorting by other columns.

Note that it is only possible to sort grid data by clicking the column header if the column headers are displayed, that is, the grid view’s OptionsView.ShowColumnHeaders property must be True. If this property is False, the column headers are hidden, so it is impossible to simulate clicks on them.

With TestComplete, you can simulate usual clicks as well as Shift- and Ctrl-clicks on the XtraGrid column headers using the Click action applied to the grid control. It requires you to know the coordinates of the column header cell within the grid. You can obtain the headers using the following statement:

view.ViewInfo.ColumnsInfo.Item_2(ColIndex).CaptionRect

Here, view is the object corresponding to the grid view, and ColIndex is the index of the needed column within the ColumnsInfo collection. Since it may not be the same as the column’s position within the view, you may need to search for the needed column in this colelction.

Below is an example that demonstrates how you can calculate the column header coordinates and simulate a click on them. The example works with the GridTutorials sample application that comes withe Developer Express XtraGrid Suite. The example contains three routines:

  • Main - The “main” routine. It obtains the scripting object corresponding to the XtraGrid control and calls ClickColumnHeader to simulate clicks on grid column headers.
  • ClickColumnHeader - Simulates a mouse click on the grid column header. The routine has the following parameters:
    • Grid - The tested XtraGrid control.
    • View - Specifies grid view that contains the column. If the the null value (Nothing is passed in the View parameter, it is assumed that the column belongs to the grid’s main view.
    • ColumnId - The column’s caption or its zero-based visible position within the view.
    • Shift - Sets whether the Shift, Alt or Ctrl keys are pressed during the click event. This parameter can have one of the following values: skNoShift (no shift keys are pressed), skShift, skCtrl or skAlt.
  • GetColumnHeaderBounds - A helper function that returns coordinates of the specified column’s header. Its Grid, View and ColumnId parameters have the same meaning as those used in the ClickColumnHeader routine.

Using the Column's SortOrder Property

It is possible to sort the data displayed in the XtraGrid control using its internal properties and methods. Each XtraGrid column has the SortOrder property that specifies the column’s sort order. You can use this property to determine or set the sort order. When setting this property in scripts, you can use one of the following string values: “Ascending”, “Descending” or “None”

This approach is universal for the XtraGrid control and does not depend on the layout type used by the grid and it works for all supported layouts (grid, banded, card and others).

The following example illustrates how to sort the XtraGrid data using the column’s SortOrder property. The example works with the same GridTutorials application and contains the following routines:

  • Main is the main routine. It obtains the scripting object corresponding to the XtraGrid control and calls Sort to sort the grid data by some columns.
  • The Sort routine sorts the grid data by the specified column. The routine has four parameters:
    • Grid - The tested XtraGrid control.
    • View - Specifies grid view that contains the column. If the the null value (Nothing is passed in the View parameter, it is assumed that the column belongs to the grid’s main view.
    • ColumnId - The column’s caption or its zero-based visible position within the view.
    • SortOrder - Specifies the sort direction. Can be “Ascending”, “Descending” or “None”.
  • GetColumn is a helper function that returns the column object by its caption or visible index.


Close

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox

By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading