C# 操作DataTable, 创建, 合并, 排序

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

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

            DataTable dt2 = new DataTable();
            dt2.Columns.Add("id", typeof(int));
            dt2.Columns.Add("title", typeof(string));

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

            // dt1合并dt2数据
            dt1.Merge(dt2);

            // DataTable 排序
            dt1.DefaultView.Sort = "id asc";
            DataTable dtSort = dt1.DefaultView.ToTable();
        }


热门推荐