迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 >

在 C# 中对 DataTable 进行排序

作者:迹忆客 最近更新:2024/01/03 浏览次数:

本教程将介绍在 C# 中对数据表进行排序的方法。


使用 C# 中的 DataView.Sort 属性对 DataTable 进行排序

DataView.Sort 属性用于获取或设置 C# 中数据表的排序列。我们可以通过指定列名来设置数据表的排序列,如 DataView.Sort = "Col_name"。默认情况下,此方法以升序对数据表进行排序。我们可以在列名之后指定 desc 来对数据表进行降序排序。然后我们可以使用 C# 中的 DataView.ToTable() 函数将此 DataView 转换为 DataTable。以下代码示例向我们展示了如何使用 C# 中的 DataView.Sort 属性对数据表进行排序。

using System;
using System.Data;

namespace datatable_sort {
  class Program {
    static void Main(string[] args) {
      DataTable table1 = new DataTable();
      DataColumn column1 = new DataColumn();
      DataColumn column2 = new DataColumn();
      column1.DataType = System.Type.GetType("System.Decimal");
      column2.DataType = System.Type.GetType("System.Decimal");
      column1.ColumnName = "Price";
      column2.ColumnName = "Rating";
      table1.Columns.Add(column1);
      table1.Columns.Add(column2);
      DataRow row;
      for (int i = 0; i < 3; i++) {
        row = table1.NewRow();
        row["Price"] = i + 1;
        row["Rating"] = i;
        table1.Rows.Add(row);
      }
      // Displaying Original Values
      Console.WriteLine("UnSorted Values");
      foreach (DataRow r in table1.Rows) {
        Console.WriteLine("Price = {0}, Rating = {1}", r[0], r[1]);
      }

      // Sorting the Table
      DataView dv = table1.DefaultView;
      dv.Sort = "Price desc";
      DataTable sortedtable1 = dv.ToTable();

      // Displaying Sorted Values
      Console.WriteLine("Sorted Values by Descending order of Price");
      foreach (DataRow r in sortedtable1.Rows) {
        Console.WriteLine("Price = {0}, Rating = {1}", r[0], r[1]);
      }
    }
  }
}

输出:

UnSorted Values
Price = 1, Rating = 0
Price = 2, Rating = 1
Price = 3, Rating = 2
Sorted Values by Descending order of Price
Price = 3, Rating = 2
Price = 2, Rating = 1
Price = 1, Rating = 0

在上面的代码中,我们首先创建了一个数据表 table1,并向其添加了两列 PriceRating。然后,我们按照 Price 的降序对表格进行排序,并显示新值。我们创建了 DataView 类的新实例,并使用 DataView.Sort 属性对其进行了排序。然后,我们使用 C# 中的 DataView.ToTable() 函数将排序后的 DataView 转换为 DataTable。最后,我们在排序表中显示了数据。


使用 C# 中的 DataTable.DefaultView 属性对 DataTable 进行排序

DataTable.DefaultView 属性用于获取 C# 中数据表的自定义视图。我们可以通过在 DataTable.DefaultView.Sort 属性中指定排序列来对数据表进行排序。默认情况下,此方法以升序对数据表进行排序。我们可以在列名之后指定 desc 来对数据表进行降序排序。然后,我们可以使用 C# 中的 DefaultView.ToTable() 函数将排序后的视图转换为数据表。以下代码示例向我们展示了如何使用 C# 中的 DataTable.DefaultView 属性对数据表进行排序。

using System;
using System.Data;

namespace datatable_sort {
  class Program {
    static void Main(string[] args) {
      DataTable table1 = new DataTable();
      DataColumn column1 = new DataColumn();
      DataColumn column2 = new DataColumn();
      column1.DataType = System.Type.GetType("System.Decimal");
      column2.DataType = System.Type.GetType("System.Decimal");
      column1.ColumnName = "Price";
      column2.ColumnName = "Rating";
      table1.Columns.Add(column1);
      table1.Columns.Add(column2);
      DataRow row;
      for (int i = 0; i < 3; i++) {
        row = table1.NewRow();
        row["Price"] = i + 1;
        row["Rating"] = i;
        table1.Rows.Add(row);
      }
      // Displaying Original Values
      Console.WriteLine("UnSorted Values");
      foreach (DataRow r in table1.Rows) {
        Console.WriteLine("Price = {0}, Rating = {1}", r[0], r[1]);
      }

      // Sorting the Table
      table1.DefaultView.Sort = "Price desc";
      table1 = table1.DefaultView.ToTable(true);

      // Displaying Sorted Values
      Console.WriteLine("Sorted Values by Descending order of Price");
      foreach (DataRow r in table1.Rows) {
        Console.WriteLine("Price = {0}, Rating = {1}", r[0], r[1]);
      }
    }
  }
}

输出:

UnSorted Values
Price = 1, Rating = 0
Price = 2, Rating = 1
Price = 3, Rating = 2
Sorted Values by Descending order of Price
Price = 3, Rating = 2
Price = 2, Rating = 1
Price = 1, Rating = 0

在上面的代码中,我们首先创建了一个数据表 table1,并添加了 PriceRating 两列。然后,我们按照 Price 的降序对 table1 表进行排序,并显示新值。我们使用 table1.DefaultView.Sort 属性创建了 table1 表的排序视图。然后,我们使用 C# 中的 table1.DefaultView.ToTable(true) 函数将排序后的视图转换为表。最后,我们在排序表中显示了数据。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在 C# 中发出 HTTP POST Web 请求

发布时间:2024/02/04 浏览次数:131 分类:编程语言

在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求

在 C# 中调整图像大小

发布时间:2024/02/04 浏览次数:203 分类:编程语言

有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保

在 C# 中下载图片

发布时间:2024/02/04 浏览次数:138 分类:编程语言

有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据

在 C# 中使用秒表

发布时间:2024/02/04 浏览次数:139 分类:编程语言

我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。

在 C# 中获取可执行路径

发布时间:2024/02/04 浏览次数:200 分类:编程语言

有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便