Export (0) Print
Expand All

BindingSource.Sort Property

Gets or sets the column names used for sorting, and the sort order for viewing the rows in the data source.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public Property Sort As String

Property Value

Type: System.String
A case-sensitive string containing the column name followed by "ASC" (for ascending) or "DESC" (for descending). The default is Nothing.

The Sort property is a case-sensitive string that specifies the column names used to sort the rows, along with the sort direction. Columns are sorted ascending by default. Multiple columns can be separated by commas, such as "State, ZipCode DESC".

To support sorting, the underlying list must implement the IBindingList or IBindingListView interfaces. This capability can be queried through the SupportsSorting property. Multicolumn sorting is available when the SupportsAdvancedSorting property is true.

Setting the Sort property will change the internal list depending on its type:

The internal list's sort properties are only changed when the sort string is not Nothing. The get accessor for this property will not retrieve the sort value of the internal list; instead, it will return the set accessor value. The value of the Sort property will persist when the data source changes.

The following example shows how to use the Sort property to perform basic sorting with a DataView. To run this example, paste the code into a Windows Form and call PopulateDataViewAndSort from the form's constructor or Load event-handling method. Your form should import the System.Xml and System.IO namespaces.

Private Sub PopulateDataViewAndSort() 
    Dim set1 As New DataSet()

    ' Some xml data to populate the DataSet with. 
    Dim musicXml As String = "<?xml version='1.0' encoding='UTF-8'?>" & _
        "<music>" & _
        "<recording><artist>Coldplay</artist><cd>X&amp;Y</cd></recording>" & _
        "<recording><artist>Dave Matthews</artist><cd>Under the Table and Dreaming</cd></recording>" & _
        "<recording><artist>Dave Matthews</artist><cd>Live at Red Rocks</cd></recording>" & _
        "<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" & _
        "<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" & _
         "</music>" 

    ' Read the xml. 
    Dim reader As New StringReader(musicXml)
    set1.ReadXml(reader)

    ' Get a DataView of the table contained in the dataset. 
    Dim tables As DataTableCollection = set1.Tables
    Dim view1 As New DataView(tables(0))

    ' Create a DataGridView control and add it to the form. 
    Dim datagridview1 As New DataGridView()
    datagridview1.AutoGenerateColumns = True 
    Me.Controls.Add(datagridview1)

    ' Create a BindingSource and set its DataSource property to 
    ' the DataView. 
    Dim source1 As New BindingSource()
    source1.DataSource = view1

    ' Set the data source for the DataGridView.
    datagridview1.DataSource = source1

    source1.Sort = "cd" 

End Sub

The following example shows how to use the Sort property to perform advanced sorting with a DataView. To run this example, paste the code into a Windows Form and call PopulateDataViewAndAdvancedSort from the form's constructor or Load event-handling method. Your form should import the System.Xml and System.IO namespaces.

Private Sub PopulateDataViewAndAdvancedSort() 
    Dim set1 As New DataSet()

    ' Some xml data to populate the DataSet with. 
    Dim musicXml As String = "<?xml version='1.0' encoding='UTF-8'?>" & _
        "<music>" & _
        "<recording><artist>Dave Matthews</artist><cd>Under the Table and Dreaming</cd></recording>" & _
        "<recording><artist>Coldplay</artist><cd>X&amp;Y</cd></recording>" & _
        "<recording><artist>Dave Matthews</artist><cd>Live at Red Rocks</cd></recording>" & _
        "<recording><artist>U2</artist><cd>Joshua Tree</cd></recording>" & _
        "<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" & _
        "<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" & _
        "</music>" 

    ' Read the xml. 
    Dim reader As New StringReader(musicXml)
    set1.ReadXml(reader)

    ' Get a DataView of the table contained in the dataset. 
    Dim tables As DataTableCollection = set1.Tables
    Dim view1 As New DataView(tables(0))

    ' Create a DataGridView control and add it to the form. 
    Dim datagridview1 As New DataGridView()
    datagridview1.AutoGenerateColumns = True 
    Me.Controls.Add(datagridview1)

    ' Create a BindingSource and set its DataSource property to 
    ' the DataView. 
    Dim source1 As New BindingSource()
    source1.DataSource = view1

    ' Set the data source for the DataGridView.
    datagridview1.DataSource = source1

    source1.Sort = "artist ASC, cd ASC"

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft