The Orientation property on the scroll event args lets you know the scroll direction. Accessing these ScrollBar controls directly allow you to have finer control over scrolling.
The diagram highlights these properties and their values at this state. You can modify the SortMode property of specific columns to allow users to sort by other column types when it makes sense to do so.
You can also sort the data programmatically by any column, or by multiple columns. DataGridView columns have three sort modes.
The sort mode for each column is specified through the SortMode property of the column, which can be set to one of the following DataGridViewColumnSortMode enumeration values. Unless column headers are used for selection, clicking the column header automatically sorts the DataGridView by this column and displays a glyph indicating the sort order. NotSortable Default for non—text box columns. You can sort this column programmatically; however, it is not intended for sorting, so no space is reserved for the sorting glyph.
Programmatic You can sort this column programmatically, and space is reserved for the sorting glyph. You might want to change the sort mode for a column that defaults to NotSortable if it contains values that can be meaningfully ordered.
For example, if you have a database column containing numbers that represent item states, you can display these numbers as corresponding icons by binding an image column to the database column.
You can then change the numerical cell values into image display values in a handler for the CellFormatting event. In this case, setting the SortMode property to Automatic will enable your users to sort the column. Automatic sorting will enable your users to group items that have the same state even if the states corresponding to the numbers do not have a natural sequence. Check box columns are another example where automatic sorting is useful for grouping items in the same state.
You can sort a DataGridView programmatically by the values in any column or in multiple columns, regardless of the SortMode settings. Programmatic sorting is useful when you want to provide your own user interface UI for sorting or when you want to implement custom sorting. Providing your own sorting UI is useful, for example, when you set the DataGridView selection mode to enable column header selection.
In this case, although the column headers cannot be used for sorting, you still want the headers to display the appropriate sorting glyph, so you would set the SortMode property to Programmatic. Columns set to programmatic sort mode do not automatically display a sorting glyph. This is necessary if you want flexibility in custom sorting. For example, if you sort the DataGridView by multiple columns, you might want to display multiple sorting glyphs or no sorting glyph.
Although you can programmatically sort a DataGridView by any column, some columns, such as button columns, might not contain values that can be meaningfully ordered. For these columns, a SortMode property setting of NotSortable indicates that it will never be used for sorting, so there is no need to reserve space in the header for the sorting glyph.
When a DataGridView is sorted, you can determine both the sort column and the sort order by checking the values of the SortedColumn and SortOrder properties. These values are not meaningful after a custom sorting operation. For more information about custom sorting, see the Custom Sorting section later in this topic.
When a DataGridView control containing both bound and unbound columns is sorted, the values in the unbound columns cannot be maintained automatically. To maintain these values, you must implement virtual mode by setting the VirtualMode property to true and handling the CellValueNeeded and CellValuePushed events.
This overload is useful when sorting by columns with values that can be meaningfully ordered, but which you do not want to configure for automatic sorting. Automatic, the SortedColumn and SortOrder properties are set automatically and the appropriate sorting glyph appears in the column header. Additionally, when the VirtualMode property is true, you can call this overload only for bound columns.
To determine whether a column is data-bound, check the IsDataBound property value. Sorting unbound columns in bound mode is not supported. The Sort IComparer method overload takes an instance of a class that implements the IComparer interface as a parameter.
This overload is useful when you want to provide custom sorting; for example, when the values in a column do not have a natural sort order or when the natural sort order is inappropriate.
In this case, you cannot use automatic sorting, but you might still want your users to sort by clicking the column headers. You can call this overload in a handler for the ColumnHeaderMouseClick event if you do not use column headers for selection. The Sort IComparer method overload works only when the DataGridView control is not bound to an external data source and the VirtualMode property value is false. To customize sorting for columns bound to an external data source, you must use the sorting operations provided by the data source.
In virtual mode, you must provide your own sorting operations for unbound columns. To use the Sort IComparer method overload, you must create your own class that implements the IComparer interface. This interface requires your class to implement the IComparer. With this, you can calculate the correct row ordering based on the values in any column. SortGlyphDirection property to display the sorting glyph. As an alternative to the Sort IComparer method overload, you can provide custom sorting by implementing a handler for the SortCompare event.
This event occurs when users click the headers of columns configured for automatic sorting or when you call the Sort DataGridViewColumn,ListSortDirection overload of the Sort method. The event occurs for each pair of rows in the control, enabling you to calculate their correct order. The SortCompare event does not occur when the DataSource property is set or when the VirtualMode property value is true. You can modify the gridline color and the control border style in addition to the border styles for the cells within the control.
The gridline color is controlled via the GridColor property. You can also apply different cell border styles for ordinary cells, row header cells, and column header cells. For advanced border styles the DataGridView provides the advanced border style properties as well. The other values of these enumerations use colors specified by the operating system.
Additionally, when visual styles are enabled on Windows XP and above, the GridColor property value is not used. The following table identifies the standard border styles available via the: