关于dataTable中根据列排序正确姿势做个随笔,方便查阅
1 System.Data.DataTable dt = new System.Data.DataTable(); 2 dt.Columns.Add("date", typeof(System.String)); 3 System.Data.DataRow dr = dt.NewRow(); 4 dr["date"] = "2016-09"; 5 System.Data.DataRow dr1 = dt.NewRow(); 6 dr1["date"] = "2015-09"; 7 System.Data.DataRow dr2 = dt.NewRow(); 8 dr2["date"] = "2014-09"; 9 System.Data.DataRow dr3 = dt.NewRow(); 10 dr3["date"] = "2016-06"; 11 System.Data.DataRow dr4 = dt.NewRow(); 12 dr4["date"] = "2013-10"; 13 dt.Rows.Add(dr); 14 dt.Rows.Add(dr1); 15 dt.Rows.Add(dr2); 16 dt.Rows.Add(dr3); 17 dt.Rows.Add(dr4); 18 System.Data.DataView dv = dt.DefaultView; 19 dv.Sort = "date"; 20 System.Data.DataTable dt1 = dv.ToTable(); 21 22 foreach (System.Data.DataRow varDr in dt1.Rows) 23 { 24 Console.WriteLine(varDr["date"].ToString()); 25 }
时间: 2024-11-12 22:47:21