Back to Devexpress

GridColumn.SortIndex Property

windowsforms-devexpress-dot-xtragrid-dot-columns-dot-gridcolumn-ef4746cf.md

latest5.5 KB
Original Source

GridColumn.SortIndex Property

Gets or sets the column’s position among sorted columns.

Namespace : DevExpress.XtraGrid.Columns

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[Browsable(false)]
[DefaultValue(-1)]
[XtraSerializablePropertyId(2)]
public int SortIndex { get; set; }
vb
<DefaultValue(-1)>
<Browsable(False)>
<XtraSerializablePropertyId(2)>
Public Property SortIndex As Integer

Property Value

TypeDefaultDescription
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.

csharp
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;
    }

}
vb
Private gridView1.CustomDrawColumnHeader += AddressOf GridView1_CustomDrawColumnHeader

Private Sub GridView1_CustomDrawColumnHeader(ByVal sender As Object, ByVal e As ColumnHeaderCustomDrawEventArgs)
    If e.Column IsNot Nothing AndAlso e.Column.SortIndex >= 0 Then
        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
    End If

End Sub

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.

winforms-grid-always-sort-by-column/CS/Form1.cs#L44

csharp
{
    if (column.SortOrder != DevExpress.Data.ColumnSortOrder.Ascending || column.SortIndex != view.SortInfo.Count - 1)
    {

winforms-grid-always-sort-by-column/VB/Form1.vb#L38

vb
If checkEdit1.Checked Then
    If column.SortOrder <> DevExpress.Data.ColumnSortOrder.Ascending OrElse column.SortIndex <> view.SortInfo.Count - 1 Then
        column.SortIndex = view.SortInfo.Count

See Also

SortedColumns

SortInfo

SortOrder

FieldNameSortGroup

Sort Data in Code

Summaries

GridColumn Class

GridColumn Members

DevExpress.XtraGrid.Columns Namespace