惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
Check Point Blog
Hacker News: Ask HN
Hacker News: Ask HN
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
WordPress大学
WordPress大学
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
N
Netflix TechBlog - Medium
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 聂微东
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 叶小钗
Cisco Talos Blog
Cisco Talos Blog
S
Schneier on Security
T
Threat Research - Cisco Blogs
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
The Hacker News
The Hacker News
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
N
News | PayPal Newsroom
L
LINUX DO - 最新话题
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Palo Alto Networks Blog
T
Tenable Blog
S
Secure Thoughts
T
Threatpost
V2EX - 技术
V2EX - 技术
大猫的无限游戏
大猫的无限游戏
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Vercel News
Vercel News
罗磊的独立博客
P
Privacy & Cybersecurity Law Blog
Engineering at Meta
Engineering at Meta
小众软件
小众软件
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
Y
Y Combinator Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
L
Lohrmann on Cybersecurity
P
Privacy International News Feed
H
Heimdal Security Blog
量子位
B
Blog

博客园 - 代码泪

VC2008下配置boost库使用正则表达式[转] 解决下载/上传大文件问题(IIS6)[转] C#格式化字符串,日期,时间,货币[转] Winform数据库连接app.config文件配置 C# 连接SQL数据库 常用连接字符串 sql得到当前系统时间得 日期部分 update select DataTime.Now.Ticks精确的时间单位[转] [SqlException (0x80131904): 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。……(provider: TCP 提供程序, error: 0 - 由于目标机器积极拒绝,无法连接。)] VS 网站发布失败 [收藏]"Automation服务器不能创建对象" 的多种解决办法 Hashtable, ArrayList, List, Dictionary学习[转] VSTS for Testers学习笔记目录[转] SQL Server 2005安全配置[转] 超时时间已到.错误及Max Pool Size设置 execute sp_executesql 用变量获取返回值【转】 使用存储过程并返回值与及返回值的获得方法[转] js取得dropdownlist的text,value[转] - 代码泪 - 博客园 input text 的事件及方法
动态创建DataTable[转]
代码泪 · 2011-08-11 · via 博客园 - 代码泪

DataTable添加列和行的方法

C#  
方法一:

DataTable  tblDatas = newDataTable("Datas");
DataColumn dc = null;

//赋值给dc,是便于对每一个datacolumn的操作
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 = newDataTable("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(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_r_r = "price *0.0862";
// Create third column.
DataColumn totalColumn = new DataColumn();
totalColumn.DataType = System.Type.GetType("System.Decimal");
totalColumn.ColumnName = "total";
//该列的表达式,值是得到的是第一列和第二列值得和
totalColumn.expression_r_r = "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放在试图中
 DataViewview = new DataView(table);
dg.DataSource = view;

dg.DataBind();