asp DataTable添加列和行的三種方法
更新時(shí)間:2009年12月05日 23:31:30 作者:
DataTable添加列和行的方法,大家可以參考下下面的代碼。
復(fù)制代碼 代碼如下:
#region 方法一:
DataTable tblDatas = new DataTable("Datas");
DataColumn dc = null;
dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
dc.AutoIncrement = true;//自動(dòng)增加
dc.AutoIncrementSeed = 1;//起始為1
dc.AutoIncrementStep = 1;//步長(zhǎng)為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"] = "大話(huà)西游";
newRow["Version"] = "2.0";
newRow["Description"] = "我很喜歡";
tblDatas.Rows.Add(newRow);
newRow = tblDatas.NewRow();
newRow["Product"] = "夢(mèng)幻西游";
newRow["Version"] = "3.0";
newRow["Description"] = "比大話(huà)更幼稚";
tblDatas.Rows.Add(newRow);
#endregion
復(fù)制代碼 代碼如下:
#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
復(fù)制代碼 代碼如下:
#region 方法三:
DataTable table = new DataTable();
//創(chuàng)建table的第一列
DataColumn priceColumn = new DataColumn();
priceColumn.DataType = System.Type.GetType("System.Decimal");//該列的數(shù)據(jù)類(lèi)型
priceColumn.ColumnName = "price";//該列得名稱(chēng)
priceColumn.DefaultValue = 50;//該列得默認(rèn)值
// 創(chuàng)建table的第二列
DataColumn taxColumn = new DataColumn();
taxColumn.DataType = System.Type.GetType("System.Decimal");
taxColumn.ColumnName = "tax";//列名
taxColumn.Expression = "price * 0.0862";//設(shè)置該列得表達(dá)式,用于計(jì)算列中的值或創(chuàng)建聚合列
// 創(chuàng)建table的第三列
DataColumn totalColumn = new DataColumn();
totalColumn.DataType = System.Type.GetType("System.Decimal");
totalColumn.ColumnName = "total";
totalColumn.Expression = "price + tax";//該列的表達(dá)式,是第一列和第二列值得和
// 將所有的列添加到table上
table.Columns.Add(priceColumn);
table.Columns.Add(taxColumn);
table.Columns.Add(totalColumn);
//創(chuàng)建一行
DataRow row = table.NewRow();
table.Rows.Add(row);//將此行添加到table中
//將table放在試圖中
DataView view = new DataView(table);
//綁定到DataGrid
dg.DataSource = view;
dg.DataBind();
#endregion
相關(guān)文章
ASP.net中網(wǎng)站訪問(wèn)量統(tǒng)計(jì)方法代碼
這篇文章介紹了ASP.net中網(wǎng)站訪問(wèn)量統(tǒng)計(jì)方法代碼,有需要的朋友可以參考一下2013-11-11
擴(kuò)展 Entity Framework支持復(fù)雜的過(guò)濾條件(多個(gè)關(guān)鍵字模糊匹配)
之前遇到一個(gè)棘手的Linq to EF查詢(xún)的技術(shù)問(wèn)題,現(xiàn)有產(chǎn)品表Product,需要根據(jù)多個(gè)關(guān)鍵字模糊匹配產(chǎn)品名稱(chēng), 現(xiàn)將解決方案分享出來(lái),按興趣的朋友可以參考下2012-12-12
使用grpcui測(cè)試ASP.NET core的gRPC服務(wù)
這篇文章介紹了使用grpcui測(cè)試ASP.NET core gRPC服務(wù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
Asp.net mvc實(shí)時(shí)生成縮率圖到硬盤(pán)
這篇文章主要介紹了Asp.net mvc實(shí)時(shí)生成縮率圖到硬盤(pán)的相關(guān)資料,需要的朋友可以參考下2016-05-05
.NET 與樹(shù)莓派WS28XX 燈帶的顏色漸變動(dòng)畫(huà)效果的實(shí)現(xiàn)
所謂顏色漸變動(dòng)畫(huà),首先,你要確定兩種顏色——起始色和最終色,比如從綠色變成紅色,綠色是起始,紅色是終點(diǎn)。這篇文章主要介紹了.NET 與樹(shù)莓派WS28XX 燈帶的顏色漸變動(dòng)畫(huà),需要的朋友可以參考下2021-12-12
一步一步學(xué)asp.net Ajax登錄設(shè)計(jì)實(shí)現(xiàn)解析
做一個(gè)登錄,擁有自動(dòng)記住賬號(hào)和密碼的功能,要保證安全性,ajax,無(wú)刷新,良好的用戶(hù)體驗(yàn).(母板頁(yè))2012-05-05
SqlCommandBuilder如何實(shí)現(xiàn)批量更新
這篇文章主要介紹了SqlCommandBuilder如何實(shí)現(xiàn)批量更新,需要的朋友可以參考下2015-10-10

