GridColumn.SortIndex Property
Gets or sets the column’s position among sorted columns.
Namespace: DevExpress.XtraGrid.Columns
Assembly: DevExpress.XtraGrid.v24.2.dll
Declaration
[Browsable(false)]
[DefaultValue(-1)]
[XtraSerializablePropertyId(2)]
public int SortIndex { get; set; }
Property Value
Type | Default | Description |
---|---|---|
Int32 | -1 | The column’s index among sorted columns. -1 if data is not sorted by this column. |
Remarks
Grid Views can sort data by multiple columns. Once a column is sorted, the View automatically adds it to the ColumnView.SortedColumns collection. If no columns were previously sorted, the column becomes the only element in the collection and gets the 0 sort index. If data is sorted by one more column, the new column is appended to the collection and gets the 1 index, etc.
If the column’s SortIndex property value changes from -1 to a non-negative integer, the column is added to the sorted column list. The column’s GridColumn.SortOrder property is automatically set to ColumnSortOrder.Ascending. Inversely, if you set a column’s SortIndex property to -1, data is not sorted by this column.
If a column has been sorted and you want to change its sort order, use the methods from the ColumnView.SortInfo object (for instance, the GridColumnSortInfoCollection.Insert method).
See Sorting in Code to learn more.
The code sample below illustrates how to show the sort index in the header row.
gridView1.CustomDrawColumnHeader += GridView1_CustomDrawColumnHeader;
private void GridView1_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column != null && e.Column.SortIndex >= 0)
{
e.DefaultDraw();
e.Cache.DrawString(e.Column.SortIndex.ToString(), e.Appearance.GetFont(), Brushes.Red, new Point(e.Bounds.Right - 20, e.Bounds.Y));
e.Handled = true;
}
}
Related GitHub Examples
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the SortIndex property.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.