首页 > 编程 > C# > 正文

C#编程实现DataTable添加行的方法

2019-10-29 21:36:21
字体:
来源:转载
供稿:网友

这篇文章主要介绍了C#编程实现DataTable添加行的方法,结合两个实例形式分析了C#操作DataTable实现动态添加行的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C#编程实现DataTable添加行的方法。分享给大家供大家参考,具体如下:

方法一:

 

 
  1. DataTable tblDatas = new DataTable("Datas"); 
  2. DataColumn dc = null
  3. dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32")); 
  4. dc.AutoIncrement = true;//自动增加 
  5. dc.AutoIncrementSeed = 1;//起始为1 
  6. dc.AutoIncrementStep = 1;//步长为1 
  7. dc.AllowDBNull = false;// 
  8. dc = tblDatas.Columns.Add("Product", Type.GetType("System.String")); 
  9. dc = tblDatas.Columns.Add("Version", Type.GetType("System.String")); 
  10. dc = tblDatas.Columns.Add("Description", Type.GetType("System.String")); 
  11. DataRow newRow; 
  12. newRow = tblDatas.NewRow(); 
  13. newRow["Product"] = "水果刀"
  14. newRow["Version"] = "2.0"
  15. newRow["Description"] = "打架专用"
  16. tblDatas.Rows.Add(newRow); 
  17. newRow = tblDatas.NewRow(); 
  18. newRow["Product"] = "折叠凳"
  19. newRow["Version"] = "3.0"
  20. newRow["Description"] = "行走江湖七武器之一"
  21. tblDatas.Rows.Add(newRow); 

方法二:

 

 
  1. DataTable tblDatas = new DataTable("Datas"); 
  2. tblDatas.Columns.Add("ID", Type.GetType("System.Int32")); 
  3. tblDatas.Columns[0].AutoIncrement = true
  4. tblDatas.Columns[0].AutoIncrementSeed = 1; 
  5. tblDatas.Columns[0].AutoIncrementStep = 1; 
  6. tblDatas.Columns.Add("Product", Type.GetType("System.String")); 
  7. tblDatas.Columns.Add("Version", Type.GetType("System.String")); 
  8. tblDatas.Columns.Add("Description", Type.GetType("System.String")); 
  9. tblDatas.Rows.Add(new object[]{null,"a","b","c"}); 
  10. tblDatas.Rows.Add(new object[] { null"a""b""c" }); 
  11. tblDatas.Rows.Add(new object[] { null"a""b""c" }); 
  12. tblDatas.Rows.Add(new object[] { null"a""b""c" }); 
  13. tblDatas.Rows.Add(new object[] { null"a""b""c" }); 

希望本文所述对大家C#程序设计有所帮助。


注:相关教程知识阅读请移步到c#教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表