C#创建datatable
来源:互联网 发布:笔记本如何卸载软件 编辑:程序博客网 时间:2024/06/05 16:35
Asp.net 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" });
方法三:
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";
// Create third column.
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中
table.Rows.Add(row);
//将table放在试图中
DataView view = new DataView(table);
dg.DataSource = view;
dg.DataBind();
- C# 动态创建DataTable
- C# 动态创建DataTable
- C#动态创建DataTable
- C#创建datatable
- C#创建datatable
- C#创建DataTable
- c#手动创建DataTable
- C#创建DataTable
- C#创建如何创建DataTable
- VS2005 C# 创建DataTable(转)
- c#和vb.net创建datatable
- c# excel 创建Sheet并绑定DataTable
- C# 关于读取TXT创建DataTable表
- C# 依据DataTable创建透视表
- 创建 DataTable
- 创建DataTable
- 创建DATATABLE
- 创建DataTable
- ZOJ 1001
- C# XML序列化去掉XML默认的命名空间及声明头
- 最近对问题(分治法)
- MapWinGIS4.9.3的二次开发
- 在Azure 环境里安装 SharePoint 2013 系统(四)安装、配置SharePoint 2013
- C#创建datatable
- JAVA-ADT:Failed to load the JNI shared library完美解决
- 学习java同步机制 (一) 基础篇
- Android 判断网络连接(一)
- 内存中常见异常值的解释(比如0xcccccccc、0xcdcdcdcd和 0xfeeefeee 异常值 )
- 数据结构---线性表的顺序实现
- html a 标签实现打电话,发短信,发邮件,查地图功能
- Yarn的资源调度与erlang进程调度二三事
- 共阴和共阳数码管编码表