DataTable Demo.
对表操作.
DatatableDemo.cs
DatatableDemo.cs
Code
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 // Use the MakeTable function below to create a new table.
4 GridView gd = new GridView();
5 DataTable table;
6 table = MakeNamesTable();
7
8 // Once a table has been created, use the
9 // NewRow to create a DataRow.
10 DataRow row;
11 row = table.NewRow();
12
13 // Then add the new row to the collection.
14 row["fName"] = "John";
15 row["lName"] = "Smith";
16 table.Rows.Add(row);
17 DataRow row1;
18 row1 = table.NewRow();
19 row1["fName"] = "aa";
20 row1["lName"] = "bb";
21 table.Rows.Add(row1);
22
23 DataRow row2 = table.NewRow();
24 row2["fName"] = "cc";
25 row2["lName"] = "dd";
26 table.Rows.Add(row2);
27
28 DataRow row3 = table.NewRow();
29 row3["lName"] = "ee";
30 table.Rows.Add(row3);
31
32 gd.DataSource = table;
33 gd.DataBind();
34
35 form1.Controls.Add(dataGrid1);
36 form1.Controls.Add(gd);
37
38 }
39 private DataTable MakeNamesTable()
40 {
41 // Create a new DataTable titled 'Names.'
42 DataTable namesTable = new DataTable("Names");
43
44 // Add three column objects to the table.
45 DataColumn idColumn = new DataColumn();
46 idColumn.DataType = System.Type.GetType("System.Int32");
47 idColumn.ColumnName = "id";
48 idColumn.AutoIncrement = true;
49 namesTable.Columns.Add(idColumn);
50
51 DataColumn fNameColumn = new DataColumn();
52 fNameColumn.DataType = System.Type.GetType("System.String");
53 fNameColumn.ColumnName = "Fname";
54 fNameColumn.DefaultValue = "Fname";
55 namesTable.Columns.Add(fNameColumn);
56
57 DataColumn lNameColumn = new DataColumn();
58 lNameColumn.DataType = System.Type.GetType("System.String");
59 lNameColumn.ColumnName = "LName";
60 namesTable.Columns.Add(lNameColumn);
61
62 // Create an array for DataColumn objects.
63 DataColumn[] keys = new DataColumn[1];
64 keys[0] = idColumn;
65 namesTable.PrimaryKey = keys;
66
67 // Return the new DataTable.
68 return namesTable;
69 }
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 // Use the MakeTable function below to create a new table.
4 GridView gd = new GridView();
5 DataTable table;
6 table = MakeNamesTable();
7
8 // Once a table has been created, use the
9 // NewRow to create a DataRow.
10 DataRow row;
11 row = table.NewRow();
12
13 // Then add the new row to the collection.
14 row["fName"] = "John";
15 row["lName"] = "Smith";
16 table.Rows.Add(row);
17 DataRow row1;
18 row1 = table.NewRow();
19 row1["fName"] = "aa";
20 row1["lName"] = "bb";
21 table.Rows.Add(row1);
22
23 DataRow row2 = table.NewRow();
24 row2["fName"] = "cc";
25 row2["lName"] = "dd";
26 table.Rows.Add(row2);
27
28 DataRow row3 = table.NewRow();
29 row3["lName"] = "ee";
30 table.Rows.Add(row3);
31
32 gd.DataSource = table;
33 gd.DataBind();
34
35 form1.Controls.Add(dataGrid1);
36 form1.Controls.Add(gd);
37
38 }
39 private DataTable MakeNamesTable()
40 {
41 // Create a new DataTable titled 'Names.'
42 DataTable namesTable = new DataTable("Names");
43
44 // Add three column objects to the table.
45 DataColumn idColumn = new DataColumn();
46 idColumn.DataType = System.Type.GetType("System.Int32");
47 idColumn.ColumnName = "id";
48 idColumn.AutoIncrement = true;
49 namesTable.Columns.Add(idColumn);
50
51 DataColumn fNameColumn = new DataColumn();
52 fNameColumn.DataType = System.Type.GetType("System.String");
53 fNameColumn.ColumnName = "Fname";
54 fNameColumn.DefaultValue = "Fname";
55 namesTable.Columns.Add(fNameColumn);
56
57 DataColumn lNameColumn = new DataColumn();
58 lNameColumn.DataType = System.Type.GetType("System.String");
59 lNameColumn.ColumnName = "LName";
60 namesTable.Columns.Add(lNameColumn);
61
62 // Create an array for DataColumn objects.
63 DataColumn[] keys = new DataColumn[1];
64 keys[0] = idColumn;
65 namesTable.PrimaryKey = keys;
66
67 // Return the new DataTable.
68 return namesTable;
69 }