DataTable添加列和行的三种方法

JRoger 原文

复制代码
#region 方法一:
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);
#endregion
复制代码

 

复制代码
#region 方法二:
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(newobject[] { null, "a", "b", "c" });
tblDatas.Rows.Add(newobject[] { null, "a", "b", "c" });
tblDatas.Rows.Add(newobject[] { null, "a", "b", "c" });
tblDatas.Rows.Add(newobject[] { null, "a", "b", "c" });
tblDatas.Rows.Add(newobject[] { null, "a", "b", "c" });
#endregion
复制代码

 

复制代码
#region 方法三:
DataTable table =new DataTable();
//创建table的第一列
DataColumn priceColumn =new DataColumn();
priceColumn.DataType = System.Type.GetType("System.Decimal");//该列的数据类型
priceColumn.ColumnName ="price";//该列得名称
priceColumn.DefaultValue =50;//该列得默认值
// 创建table的第二列
DataColumn taxColumn =new DataColumn();
taxColumn.DataType = System.Type.GetType("System.Decimal");
taxColumn.ColumnName ="tax";//列名
taxColumn.Expression ="price * 0.0862";//设置该列得表达式,用于计算列中的值或创建聚合列
// 创建table的第三列
DataColumn totalColumn =new DataColumn();
totalColumn.DataType = System.Type.GetType("System.Decimal");
totalColumn.ColumnName ="total";
totalColumn.Expression ="price + tax";//该列的表达式,是第一列和第二列值得和
// 将所有的列添加到table上
table.Columns.Add(priceColumn);
table.Columns.Add(taxColumn);
table.Columns.Add(totalColumn);
//创建一行
DataRow row = table.NewRow();
table.Rows.Add(row);//将此行添加到table中
//将table放在视图中
DataView view =new DataView(table);
//绑定到DataGrid
dg.DataSource = view;
dg.DataBind();
#endregion
复制代码

 

 

 

没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。



    本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/6006488.html ,如需转载请自行联系原作者




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值