Sorting a Data Table

后端 未结 5 1891
借酒劲吻你
借酒劲吻你 2020-12-01 17:47

I tried to sort a data table with following two ways

table.DefaultView.Sort = \"Town ASC, Cutomer ASC\"

table.Select(\"\", \"Town ASC, Cutomer ASC\")
         


        
5条回答
  •  一个人的身影
    2020-12-01 18:37

    After setting the sort expression on the DefaultView (table.DefaultView.Sort = "Town ASC, Cutomer ASC" ) you should loop over the table using the DefaultView not the DataTable instance itself

    foreach(DataRowView r in table.DefaultView)
    {
        //... here you get the rows in sorted order
        Console.WriteLine(r["Town"].ToString());
    }
    

    Using the Select method of the DataTable instead, produces an array of DataRow. This array is sorted as from your request, not the DataTable

    DataRow[] rowList = table.Select("", "Town ASC, Cutomer ASC");
    foreach(DataRow r in rowList)
    {
        Console.WriteLine(r["Town"].ToString());
    }
    

提交回复
热议问题