Sorting Data in Microsoft DataGridView

  March 02, 2007

The DataGridView control includes a data sorting feature. To sort the data by a particular column, the user clicks on the header of that

column (once or twice, depending on the desired sort order). Sorting can also be performed programmatically by calling the DataGridView.Sort method. In your tests, you can use the approach that best suits your needs.

Simulating Column Headers Clicks

It is possible to sort the data displayed in the DataGridView 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). However, this functionality is only enabled if the grid’s SelectionMode property is set to something other than ColumnHeaderSelect or FullColumnSelect. Otherwise, clicking the column header selects the entire column and it is impossible to sort grid data using column header clicks. Also, the column headers in the grid are only displayed if the ColumnsHeadersVisible property of the grid object is True. Otherwise, if this property is False, the grid headers are hidden and it is impossible to simulate clicks on them. In this case, you can implement sorting using the grid’s internal properties and methods.

In TestComplete scripts, you can simulate clicks on onscreen objects using the special Click action. It requires you to know the coordinates of the point within the grid control where you want to click, that is the coordinates of the desired column header cell.

You can get the object corresponding to the column header using the column’s HeaderCell property, as described in Selecting Cells in Microsoft DataGridView. To determine the screen-related cell coordinates, you can use its AccessibilityObject property. You can then convert them to grid-related coordinates and pass them to the Click action.

The example below illustrates this approach. It contains the following routines:

  • Main is the main routine. It obtains the scripting object corresponding to the DataGridView control and calls ClickColumnHeader to click column headers to sort the grid data by those columns.
  • The ClickColumnHeader routine simulates the column header click. The Grid parameter specified the grid control to which the column belongs, and ColumnId specifies either the desired column index or its name (which is defined in the application code).
  • GetColumn is a helper routine that returns the column object by its index or name (it is implemented so that the ColumnId parameter can take either string or integer values).

Using the DataGridView.Sort Method

If the first approach does not suit your needs for some reason (for example, the column headers are invisible so that you can’t click them), you can sort the DataGridView data using the grid’s internal members.

DataGridView has a special Sort method that can be used to sort data in the grid. You can read about this method in the following MSDN atricle – DataGridView.Sort Method.

The method declaration is as follows:

gridObj.Sort (dataGridViewColumn, direction)

The dataGridViewColumn parameter specifies the grid column by which the grid contents will be sorted. The sort direction is specified by the direction parameter. In scripts, you can pass 0 or “Ascending” to specify the ascending direction; 1 or “Descending” – the descending direction.

The following example demonstarates how to use the Sort method to sort the grid data. The example includes the following routines:

  • The Main routine that obtains the scripting object corresponding to the DataGridView control and calls the Sort routine to sort grid data by two columns.
  • The Sort routine that uses the DataGridView.Sort method to sort data in the Grid control by the specified Column. The sort direction is specified by the SortOrder parameter.

Note however, that using the DataGridView.Sort method may not be admissible in some cases. For example, if the tested application implements a custom sort algorithm or allows sorting data by several columns, calling the DataGridView.Sort method directly may not give you the desired result. In that case, you should ask the tested application’s developers whether you can use this method to sort the grid data.

See also: