C# DataTable添加行 C#编程实现DataTable添加行的方法
Jan.David 人气:0想了解C#编程实现DataTable添加行的方法的相关内容吗,Jan.David在本文为您仔细讲解C# DataTable添加行的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,DataTable,添加行,下面大家一起来学习吧。
本文实例讲述了C#编程实现DataTable添加行的方法。分享给大家供大家参考,具体如下:
方法一:
DataTable tblDatas = new DataTable("Datas"); DataColumn dc = null; dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32")); dc.AutoIncrement = true;//自动增加 dc.AutoIncrementSeed = 1;//起始为1 dc.AutoIncrementStep = 1;//步长为1 dc.AllowDBNull = false;// dc = tblDatas.Columns.Add("Product", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Version", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Description", Type.GetType("System.String")); DataRow newRow; newRow = tblDatas.NewRow(); newRow["Product"] = "水果刀"; newRow["Version"] = "2.0"; newRow["Description"] = "打架专用"; tblDatas.Rows.Add(newRow); newRow = tblDatas.NewRow(); newRow["Product"] = "折叠凳"; newRow["Version"] = "3.0"; newRow["Description"] = "行走江湖七武器之一"; tblDatas.Rows.Add(newRow);
方法二:
DataTable tblDatas = new DataTable("Datas"); tblDatas.Columns.Add("ID", Type.GetType("System.Int32")); tblDatas.Columns[0].AutoIncrement = true; tblDatas.Columns[0].AutoIncrementSeed = 1; tblDatas.Columns[0].AutoIncrementStep = 1; tblDatas.Columns.Add("Product", Type.GetType("System.String")); tblDatas.Columns.Add("Version", Type.GetType("System.String")); tblDatas.Columns.Add("Description", Type.GetType("System.String")); tblDatas.Rows.Add(new object[]{null,"a","b","c"}); tblDatas.Rows.Add(new object[] { null, "a", "b", "c" }); tblDatas.Rows.Add(new object[] { null, "a", "b", "c" }); tblDatas.Rows.Add(new object[] { null, "a", "b", "c" }); tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
希望本文所述对大家C#程序设计有所帮助。
加载全部内容