在数据表中对行进行排序

171

我们在一个 DataTable 中有两列,就像这样:

COL1   COL2
Abc    5
Def    8
Ghi    3

我们正在尝试根据 COL2 以降序排序此 datatable

COL1            COL2
ghi             8
abc             4
def             3
jkl             1

我们尝试了这个:

ft.DefaultView.Sort = "COL2 desc";
ft = ft.DefaultView.ToTable(true);

但是,不使用 DataView,我们想要对 DataTable 本身进行排序,而不是 DataView

14个回答

3

//希望这能对您有所帮助..

        DataTable table = new DataTable();
        //DataRow[] rowArray = dataTable.Select();
        table = dataTable.Clone();
        for (int i = dataTable.Rows.Count - 1; i >= 0; i--)
        {
            table.ImportRow(dataTable.Rows[i]);
        }
        return table;

2

简短总结

使用tableObject.Select(queryExpression, sortOrderExpression)以排序方式选择数据。

完整示例

完整的工作示例 - 可在控制台应用程序中测试:

    using System;
    using System.Data;

    namespace A
    {
        class Program
        {
            static void Main(string[] args)
            {
                DataTable table = new DataTable("Orders");
                table.Columns.Add("OrderID", typeof(Int32));
                table.Columns.Add("OrderQuantity", typeof(Int32));
                table.Columns.Add("CompanyName", typeof(string));
                table.Columns.Add("Date", typeof(DateTime));

                DataRow newRow = table.NewRow();
                newRow["OrderID"] = 1;
                newRow["OrderQuantity"] = 3;
                newRow["CompanyName"] = "NewCompanyName";
                newRow["Date"] = "1979, 1, 31";

                // Add the row to the rows collection.
                table.Rows.Add(newRow);

                DataRow newRow2 = table.NewRow();
                newRow2["OrderID"] = 2;
                newRow2["OrderQuantity"] = 2;
                newRow2["CompanyName"] = "NewCompanyName1";
                table.Rows.Add(newRow2);

                DataRow newRow3 = table.NewRow();
                newRow3["OrderID"] = 3;
                newRow3["OrderQuantity"] = 2;
                newRow3["CompanyName"] = "NewCompanyName2";
                table.Rows.Add(newRow3);

                DataRow[] foundRows;

                Console.WriteLine("Original table's CompanyNames");
                Console.WriteLine("************************************");
                foundRows = table.Select();

                // Print column 0 of each returned row.
                for (int i = 0; i < foundRows.Length; i++)
                    Console.WriteLine(foundRows[i][2]);

                // Presuming the DataTable has a column named Date.
                string expression = "Date = '1/31/1979' or OrderID = 2";
                // string expression = "OrderQuantity = 2 and OrderID = 2";

                // Sort descending by column named CompanyName.
                string sortOrder = "CompanyName ASC";

                Console.WriteLine("\nCompanyNames data for Date = '1/31/1979' or OrderID = 2, sorted CompanyName ASC");
                Console.WriteLine("************************************");
                // Use the Select method to find all rows matching the filter.
                foundRows = table.Select(expression, sortOrder);

                // Print column 0 of each returned row.
                for (int i = 0; i < foundRows.Length; i++)
                    Console.WriteLine(foundRows[i][2]);

                Console.ReadKey();
            }
        }
    }

输出

输出


0

试试这个:

DataTable DT = new DataTable();
DataTable sortedDT = DT;
sortedDT.Clear();
foreach (DataRow row in DT.Select("", "DiffTotal desc"))
{
    sortedDT.NewRow();
    sortedDT.Rows.Add(row);
}
DT = sortedDT;

  1. 你必须创建新的表格 DataTable sortedDT = new DataTable()
  2. 你需要使用 ImportRow(你不能从不同的表格添加行)。
- marbel82

0

是的,以上的答案描述了对数据表排序的正确方法。

DataView dv = ft.DefaultView;
dv.Sort = "occr desc";
DataTable sortedDT = dv.ToTable();

但除此之外,要选择其中的特定行,您可以使用LINQ并尝试以下操作

var Temp = MyDataSet.Tables[0].AsEnumerable().Take(1).CopyToDataTable();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接