DataTable排序
Posted 棉晗榜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataTable排序相关的知识,希望对你有一定的参考价值。
DataTable dataTable = new DataTable();
dataTable.Columns.Add("id");
dataTable.Columns.Add("name");
dataTable.Columns.Add("weight");
dataTable.Columns.Add("birthday");
dataTable.Rows.Add(new object[] "1","欧阳修","56",DateTime.Parse("1992-02-26"));
dataTable.Rows.Add(new object[] "3","武松","22",DateTime.Parse("1987-11-15"));
dataTable.Rows.Add(new object[] "9","李斯","33",DateTime.Parse("1975-06-11"));
dataTable.Rows.Add(new object[] "5","赵武","99",DateTime.Parse("1979-08-25"));
//DataTable排序
DataTable dataView = dataTable.Rows.Cast<DataRow>().OrderBy(r => r[2]).CopyToDataTable();
DataTable dataView2 = dataTable.Rows.Cast<DataRow>().OrderBy(r => r[3]).CopyToDataTable();
DataTable dataView3 = dataTable.Rows.Cast<DataRow>().OrderByDescending(r => r[0]).CopyToDataTable();
dataTable.DefaultView.Sort = "id asc";
var dv = dataTable.DefaultView.ToTable();
以上是关于DataTable排序的主要内容,如果未能解决你的问题,请参考以下文章