DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(new object[] { 12, "lwolf" });
dt.Rows.Add(new object[] { 100,"kkkkk"});
dt.Rows.Add(new object[] { 19,"jim" });
dt.Rows.Add(new object[] { 1,"test" });
DataTable dtCopy = dt.Copy();
DataView dv = dt.DefaultView;
dv.Sort = "ID";
dtCopy = dv.ToTable();
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("CreateTime", typeof(string));
dr = dt.NewRow();
.....
dt.Rows.Add(dr);
dt.DefaultView.Sort = "CreateTime DESC";
CreateTime 是列名,ASC/DESC 升序/降序排列。
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Age");//因为是字符串,所以排序不对
dt.Rows.Add("小明", "21");
dt.Rows.Add("小张", "10");
dt.Rows.Add("小红", "9");
dt.Rows.Add("小伟", "7");
dt.Rows.Add("小美", "3");
dt.DefaultView.Sort = "Age ASC";
dt = dt.DefaultView.ToTable();
标签:Rows,C#,DataTable,Add,new,dt,排序,Datatable,Columns
From: https://www.cnblogs.com/nuomibaibai/p/17150198.html