首页 文章

对数据表中的行进行排序

提问于
浏览
114

我们在 DataTable 中有两列,如下所示:

COL1   COL2
Abc    5
Def    8
Ghi    3

我们试图按递减顺序对 datatable 进行排序 COL2 .

COL1            COL2
ghi             8
abc             4
def             3
jkl             1

我们试过这个:

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

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

12 回答

  • 13

    它的使用简单 . 选择功能 .

    DataRow[] foundRows=table.Select("Date = '1/31/1979' or OrderID = 2", "CompanyName ASC");
    DataTable dt = foundRows.CopyToDataTable();
    

    它已经完成......快乐编码

  • 17

    您是否尝试在DataTable上使用 Select(filterExpression, sortOrder) 方法?有关示例,请参见here . 请注意,此方法不会对数据表进行排序,如果这是您要查找的内容,但它将返回已排序的行数组而不使用数据视图 .

  • 4
    table.DefaultView.Sort = "[occr] DESC";
    
  • 8

    或者,如果您可以使用 DataGridView ,则只需调用 Sort(column, direction)

    namespace Sorter
    {
        using System;
        using System.ComponentModel;
        using System.Windows.Forms;
    
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.dataGridView1.Rows.Add("Abc", 5);
                this.dataGridView1.Rows.Add("Def", 8);
                this.dataGridView1.Rows.Add("Ghi", 3);
                this.dataGridView1.Sort(this.dataGridView1.Columns[1], 
                                        ListSortDirection.Ascending);
            }
        }
    }
    

    哪个会给你想要的结果:

    Debugger view

  • 17

    TL; DR

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

    完整的例子

    完成working example - 可以在_86106中测试:

    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();
                }
            }
        }
    

    输出

  • 3

    试试这个:

    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 dt = new DataTable();         
    dt.DefaultView.Sort = "Column_name desc";
    dt = dt.DefaultView.ToTable();
    
  • 4

    There is 2 way for sort data

    1)只对数据进行排序并填入网格:

    DataGridView datagridview1 = new DataGridView(); // for show data
    DataTable dt1 = new DataTable(); // have data
    DataTable dt2 = new DataTable(); // temp data table
    DataRow[] dra = dt1.Select("", "ID DESC");
    if (dra.Length > 0)
        dt2 = dra.CopyToDataTable();
    datagridview1.DataSource = dt2;
    

    2)排序默认视图,类似于使用网格列 Headers 的排序:

    DataGridView datagridview1 = new DataGridView(); // for show data
    DataTable dt1 = new DataTable(); // have data
    dt1.DefaultView.Sort = "ID DESC";
    datagridview1.DataSource = dt1;
    
  • 0

    事实证明,有一种特殊情况可以实现 . 诀窍在于构建DataTable时,收集列表中的所有行,对它们进行排序,然后添加它们 . 这个案子就在这里 .

  • 299

    我担心你不能像你想要的那样轻松地进行就地排序的DataTable .

    您可以做的是从您从原始DataTable创建的DataView创建一个新的DataTable . 在DataView上应用您想要的任何排序和/或过滤器,然后使用DataView.ToTable方法从DataView创建新的DataTable:

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

    也许以下内容可以帮助:

    DataRow[] dataRows = table.Select().OrderBy(u => u["EmailId"]).ToArray();
    

    在这里,您也可以使用其他Lambda表达式查询 .

  • 10

    //希望对你有帮助..

    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;
    

相关问题