Datagridview which column is sorted




















Privacy policy. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Data Grid View. Forms Assembly: System. The default sort mode for other column types is NotSortable. The SortMode value does not prevent you from sorting a column programmatically, although other restrictions may apply. For more information, see the Sort method. ColumnHeaderMouseClick event from occurring, but it will prevent the header from changing its appearance when it is clicked.

Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services. Privacy policy. Skip to main content. This browser is no longer supported. 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.

Sorting by unbound columns in bound mode is not supported. You can sort a DataGridView programmatically by calling its Sort method. 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.



0コメント

  • 1000 / 1000