日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

DataTable添加列和行的三种方法

發(fā)布時間:2025/7/25 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DataTable添加列和行的三种方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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(); //創(chuàng)建table的第一列 DataColumn priceColumn =new DataColumn(); priceColumn.DataType = System.Type.GetType("System.Decimal");//該列的數(shù)據(jù)類型 priceColumn.ColumnName ="price";//該列得名稱 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á)式,用于計算列中的值或創(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

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/arxive/p/6006488.html

總結(jié)

以上是生活随笔為你收集整理的DataTable添加列和行的三种方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。