DataView.Sort DataView.Sort DataView.Sort DataView.Sort Property

Definizione

Ottiene o imposta la colonna o le colonne e l'ordinamento per l'oggetto DataView.Gets or sets the sort column or columns, and sort order for the DataView.

public:
 property System::String ^ Sort { System::String ^ get(); void set(System::String ^ value); };
[System.Data.DataSysDescription("DataViewSortDescr")]
public string Sort { get; set; }
member this.Sort : string with get, set
Public Property Sort As String

Valore della proprietà

Stringa che contiene il nome della colonna seguito da "ASC" (crescente) o "DESC" (decrescente).A string that contains the column name followed by "ASC" (ascending) or "DESC" (descending). Le colonne vengono ordinate in ordine crescente per impostazione predefinita.Columns are sorted ascending by default. Più colonne possono essere separate da virgole.Multiple columns can be separated by commas.

Esempi

Nell'esempio seguente viene indicato DataView a di ordinare la tabella in base a due colonne.The following example instructs the DataView to sort the table by two columns.

using System.Data;
using System;
public class A {
   static void Main(string[] args) {
      DataTable locationTable = new DataTable("Location");
      // Add two columns
      locationTable.Columns.Add("State");
      locationTable.Columns.Add("ZipCode");

      // Add data 
      locationTable.Rows.Add("Washington", "98052");
      locationTable.Rows.Add("California", "90001");
      locationTable.Rows.Add("Hawaii", "96807");
      locationTable.Rows.Add("Hawaii", "96801");
      locationTable.AcceptChanges();

      Console.WriteLine("Rows in original order\n State \t\t ZipCode");
      foreach (DataRow row in locationTable.Rows) {
         Console.WriteLine(" {0} \t {1}", row["State"], row["ZipCode"]);
      }

      // Create DataView
      DataView view = new DataView(locationTable);

      // Sort by State and ZipCode column in descending order
      view.Sort = "State ASC, ZipCode ASC";

      Console.WriteLine("\nRows in sorted order\n State \t\t ZipCode");
      foreach (DataRowView row in view) {
         Console.WriteLine(" {0} \t {1}", row["State"], row["ZipCode"]);
      }
   }
}
Imports System.Data
Public Class A
   Public Shared Sub Main(args As String())
      Dim locationTable As New DataTable("Location")
      ' Add two columns
      locationTable.Columns.Add("State")
      locationTable.Columns.Add("ZipCode")

      ' Add data 
      locationTable.Rows.Add("Washington", "98052")
      locationTable.Rows.Add("California", "90001")
      locationTable.Rows.Add("Hawaii", "96807")
      locationTable.Rows.Add("Hawaii", "96801")
      locationTable.AcceptChanges()

      Console.WriteLine("Rows in original order" & vbLf & " State " & vbTab & vbTab & " ZipCode")
      For Each row As DataRow In locationTable.Rows
         Console.WriteLine(" {0} " & vbTab & " {1}", row("State"), row("ZipCode"))
      Next

      ' Create DataView
      Dim view As New DataView(locationTable)

      ' Sort by State and ZipCode column in descending order
      view.Sort = "State ASC, ZipCode ASC"

      Console.WriteLine(vbLf & "Rows in sorted order" & vbLf & " State " & vbTab & vbTab & " ZipCode")
      For Each row As DataRowView In view
         Console.WriteLine(" {0} " & vbTab & " {1}", row("State"), row("ZipCode"))
      Next
   End Sub
End Class

Commenti

Se non si specificano in modo esplicito i DataViewcriteri di DataRowView ordinamento per DataView , gli oggetti in sono ordinati in base all' DataRow indice di DataTable.Rows corrispondente in DataRowCollection.If you do not explicitly specify sort criteria for DataView, the DataRowView objects in DataView are sorted based on the index of its corresponding DataRow in the DataTable.Rows DataRowCollection.

Per altre informazioni, vedere DataViews.For more information, see DataViews.

Si applica a

Vedi anche