本文主要是介绍C# 将同一个DataTable复制多份到DataSet中,并对部分Table进行排序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//创建一个表DataTable dt = new DataTable("Product");DataColumn dc = null;//添加列,赋值dc = dt.Columns.Add("id", Type.GetType("System.Int32"));dc.AutoIncrement = true;dc.AutoIncrementSeed = 1;dc.AutoIncrementStep = 1;dc.AllowDBNull = false;dt.Columns.Add("pname", Type.GetType("System.String"));dt.Columns.Add("price", Type.GetType("System.Double"));dt.Columns.Add("counts", Type.GetType("System.Double"));dt.Columns.Add("memo", Type.GetType("System.String"));DataRow dr = dt.NewRow();dr["pname"] = "red apple";dr["price"] = 9.9;dr["counts"] = 9;dr["memo"] = "this red apple so red.";dt.Rows.Add(dr);dr = dt.NewRow();dr["pname"] = "black apple";dr["price"] = 19.9;dr["counts"] = 8;dr["memo"] = "this black apple so black.";dt.Rows.Add(dr);dr = dt.NewRow();dr["pname"] = "gold apple";dr["price"] = 29.9;dr["counts"] = 7;dr["memo"] = "this gold apple so gold.";dt.Rows.Add(dr);DataSet ds = new DataSet();for (int i = 0; i < 3; i++){if (i==1){dt.TableName = "Table" + i;dt.DefaultView.Sort = "price desc,counts asc ";dt = dt.DefaultView.ToTable();ds.Tables.Add(dt);}else{var renewDt = dt.Clone(); //复制表结构var dtResult = dt.AsEnumerable().AsQueryable(); //获取所有源数据行数据 if (dtResult.Count() > 0){renewDt = dtResult.CopyToDataTable(); //数据行填充到新的表结构中}renewDt.TableName = "Table" + i;ds.Tables.Add(renewDt);}}
参考链接:https://www.codeleading.com/article/23824561886/
这篇关于C# 将同一个DataTable复制多份到DataSet中,并对部分Table进行排序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!