Leer en inglés

Compartir a través de


DataView.Sort Propiedad

Definición

Obtiene o establece la columna o columnas de ordenación y el criterio de ordenación de la DataView.

C#
public string Sort { get; set; }
C#
[System.Data.DataSysDescription("DataViewSortDescr")]
public string Sort { get; set; }

Valor de propiedad

Cadena que contiene el nombre de columna seguido de "ASC" (ascendente) o "DESC" (descendente). Las columnas se ordenan de forma ascendente de forma predeterminada. Varias columnas se pueden separar por comas.

Atributos

Ejemplos

En el ejemplo siguiente se indica al DataView ordenar la tabla por dos columnas.

C#
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"]);
      }
   }
}

Comentarios

Si no especifica explícitamente criterios de ordenación para DataView, los objetos DataRowView de DataView se ordenan en función del índice de su DataRow correspondiente en el DataTable.RowsDataRowCollection.

Para obtener más información, vea DataViews.

Se aplica a

Producto Versiones
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Consulte también