标签:
方法一
DT = new DataTable(); DataRow DR = DT.NewRow();
//构建列(名称,数据类型) DT.Columns.Add("代码", Type.GetType("System.String")); DT.Columns.Add("进价", Type.GetType("System.String")); DT.Columns.Add("售价", Type.GetType("System.String")); DT.Columns.Add("原因", Type.GetType("System.String")); DT.Columns.Add("原进价", Type.GetType("System.String")); DT.Columns.Add("原售价", Type.GetType("System.String"));
//构建第一条数据 DR["代码"] = TxtCode.Text.Trim(); DR["进价"] = TxtJinPrice.Text.Trim(); DR["售价"] = TxtShouPrice.Text.Trim(); DR["原因"] = TxtYuanYin.Text.Trim(); DR["原进价"] = OldPring; DR["原售价"] = OldPrice; DT.Rows.Add(DR); a = 1;
方法二
DataTable tblDatas = newDataTable("Datas"); DataColumn dc = null; //赋值给dc,是便于对每一个datacolumn的操作 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")); //添加两条数据
//* datatable 行数据可以叠加 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 = newDataTable("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(newobject[]{null,"a","b","c"}); tblDatas.Rows.Add(newobject[] { 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" });
标签:
原文地址:http://www.cnblogs.com/hanke123/p/5629220.html