当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp DataTable添加列和行的三种方法

ASP.NET
探讨:ASP.NET技术的学习顺序问题
关于ASP.NET在IIS一些问题的经验总结
ASP.NET中通过对话框方式下载文件
ASP.NET网络编程中常用到的27个函数集
ASP.NET生成静态网页的方法
ASP.NET 2.0中实现弹窗报警提示
复杂ASP.NET服务器控件调整小技巧
ASP.NET调用oracle存储过程实现快速分页
VB.NET实现窗体图标最小化到状态栏
ASP.NET技巧:DataGrid传统分页方式
ASP.NET里的事务处理
ASP.NET 2.0高级数据处理之数据绑定
ASP.NET多频道网站架构实现方法
.NET vs J2EE——面对SOA的荒谬与误解
ASP.NET 2.0中执行数据库操作命令之一
ASP.NET应用程序资源访问安全模型
ASP.NET 2.0中的Web和HTML服务器控件
对.NET Framework 反射的反思
带你走进ASP.NET(1)
带你走进ASP.NET(2)

ASP.NET 中的 asp DataTable添加列和行的三种方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 71 ::
收藏到网摘: n/a

DataTable添加列和行的方法,大家可以参考下下面的代码。
复制代码 代码如下:

#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(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" });
#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