C#编程实现DataTable添加行的方法
程序员文章站
2022-09-05 07:50:54
本文实例讲述了c#编程实现datatable添加行的方法。分享给大家供大家参考,具体如下:
方法一:
datatable tbldatas = new data...
本文实例讲述了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#程序设计有所帮助。
下一篇: .NET Core中本地化机制的深入讲解