程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> (轉)C#創建datatable,

(轉)C#創建datatable,

編輯:C#入門知識

(轉)C#創建datatable,


 1 Asp.net DataTable添加列和行的方法
 2 方法一:
 3 
 4 DataTable tblDatas = new DataTable("Datas");
 5 DataColumn dc = null;
 6 dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
 7 dc.AutoIncrement = true;//自動增加
 8 dc.AutoIncrementSeed = 1;//起始為1
 9 dc.AutoIncrementStep = 1;//步長為1
10 dc.AllowDBNull = false;//
11 
12 dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
13 dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
14 dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
15 
16 DataRow newRow;
17 newRow = tblDatas.NewRow();
18 newRow["Product"] = "大話西游";
19 newRow["Version"] = "2.0";
20 newRow["Description"] = "我很喜歡";
21 tblDatas.Rows.Add(newRow);
22 
23 newRow = tblDatas.NewRow();
24 newRow["Product"] = "夢幻西游";
25 newRow["Version"] = "3.0";
26 newRow["Description"] = "比大話更幼稚";
27 tblDatas.Rows.Add(newRow);
28 
29 方法二:
30 
31 DataTable tblDatas = new DataTable("Datas");
32 tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
33 tblDatas.Columns[0].AutoIncrement = true;
34 tblDatas.Columns[0].AutoIncrementSeed = 1;
35 tblDatas.Columns[0].AutoIncrementStep = 1;
36 
37 tblDatas.Columns.Add("Product", Type.GetType("System.String"));
38 tblDatas.Columns.Add("Version", Type.GetType("System.String"));
39 tblDatas.Columns.Add("Description", Type.GetType("System.String"));
40 
41 tblDatas.Rows.Add(new object[]{null,"a","b","c"});
42 tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
43 tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
44 tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
45 tblDatas.Rows.Add(new object[] { null, "a", "b", "c" });
46 
47 方法三:
48 DataTable table = new DataTable ();
49 
50 //創建table的第一列
51 DataColumn priceColumn = new DataColumn();
52 //該列的數據類型
53 priceColumn.DataType = System.Type.GetType("System.Decimal");
54 //該列得名稱
55 priceColumn.ColumnName = "price";
56 //該列得默認值
57 priceColumn.DefaultValue = 50;
58 
59 // 創建table的第二列
60 DataColumn taxColumn = new DataColumn();
61 taxColumn.DataType = System.Type.GetType("System.Decimal");
62 //列名
63 taxColumn.ColumnName = "tax";
64 //設置該列得表達式,用於計算列中的值或創建聚合列
65 taxColumn.Expression = "price * 0.0862";
66 // Create third column.
67 DataColumn totalColumn = new DataColumn();
68 totalColumn.DataType = System.Type.GetType("System.Decimal");
69 totalColumn.ColumnName = "total";
70 //該列的表達式,值是得到的是第一列和第二列值得和
71 totalColumn.Expression = "price + tax";
72 
73 // 將所有的列添加到table上
74 table.Columns.Add(priceColumn);
75 table.Columns.Add(taxColumn);
76 table.Columns.Add(totalColumn);
77 
78 //創建一行
79 DataRow row = table.NewRow();
80 //將此行添加到table中
81 table.Rows.Add(row);
82 
83 //將table放在試圖中
84 DataView view = new DataView(table);
85 dg.DataSource = view;
86 
87 dg.DataBind();

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved