英語で読む

次の方法で共有


DataView.Sort プロパティ

定義

DataViewの並べ替え列または並べ替え順序を取得または設定します。

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

プロパティ値

列名の後に "ASC" (昇順) または "DESC" (降順) が続く文字列。 列は既定で昇順に並べ替えられます。 複数の列をコンマで区切ることができます。

属性

次の例では、テーブルを 2 つの列で並べ替える DataView に指示します。

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

注釈

DataViewの並べ替え基準を明示的に指定しない場合、DataView 内の DataRowView オブジェクトは、DataTable.RowsDataRowCollection内の対応する DataRow のインデックスに基づいて並べ替えられます。

詳細については、「DataViews」を参照してください。

適用対象

製品 バージョン
.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

こちらもご覧ください