位置:首页 > 编程专栏 > C#

C# 操作DataTable排序

C# 操作DataTable排序
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id", typeof(int));
            dt.Columns.Add("title", typeof(string));

            dt.Columns["id"].AutoIncrement = true;
            for (int i = 0; i < 100; ++i)
            {
                DataRow newRow = dt.NewRow();
                newRow["title"] = DateTime.Now.Ticks.ToString();
                dt.Rows.Add(newRow);
            }

            // 排序
            DataView dv = new DataView(dt);
            dv.Sort = "id desc";
            DataTable dt2 = dv.ToTable();
        }

C# 社区推荐