国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp DataTable添加列和行的三種方法

2024-07-10 13:18:35
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:


#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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 潮安县| 左云县| 泸州市| 葵青区| 金坛市| 长白| 大化| 健康| 武陟县| 兰坪| 张家口市| 砀山县| 额尔古纳市| 仁寿县| 大渡口区| 通山县| 宝兴县| 沐川县| 永丰县| 海原县| 安陆市| 红原县| 元朗区| 津南区| 周宁县| 怀化市| 绥江县| 宁陕县| 天津市| 慈利县| 荥阳市| 襄城县| 涡阳县| 吉林市| 衡水市| 炉霍县| 双峰县| 中西区| 阳朔县| 新乐市| 缙云县|