DataTable 相关

1、对表的初始化
 1      //创建表
 2         DataTable table = new DataTable();
 3         //添加列
 4         table.Columns.Add("ID", typeof(Int32));
 5         table.Columns.Add("Name", typeof(String));
 6         table.Columns.Add("Age", typeof(Int32));
 7         table.Columns.Add("CreateTime", typeof(DateTime));
 8         //添加行和数据
 9         table.Rows.Add(new object[] { 1, "张三", 20, DateTime.Now });
10         table.Rows.Add(new object[] { 2, "李四", 25, DateTime.Now });
11         table.Rows.Add(new object[] { 3, "王五", 30, DateTime.Now });

 2、添加列的一种方式

 1         DataColumn column;
 2             // Create new DataColumn, set DataType, 
 3             // ColumnName and add to DataTable.    
 4             column = new DataColumn();
 5             column.DataType = System.Type.GetType("System.Int32");
 6             column.ColumnName = "id";
 7             column.ReadOnly = true;
 8             column.Unique = true;
 9             // Add the Column to the DataColumnCollection.
10             table.Columns.Add(column);

 3、 DataViewRowState.Added 与 Datatable.AcceptChanges() 的一点关系

先上一段MSDN的实例代码

Select(String, String, DataViewRowState)

 1  static void Main(string[] args)
 2         {
 3             try
 4             {
 5                 DataTable customerTable = new DataTable("Customers");
 6                 // Add columns
 7                 customerTable.Columns.Add("id", typeof(int));
 8                 customerTable.Columns.Add("name", typeof(string));
 9 
10                 // Set PrimaryKey
11                 customerTable.Columns["id"].Unique = true;
12                 customerTable.PrimaryKey = new DataColumn[]
13                     { customerTable.Columns["id"] };
14 
15                 // Add ten rows
16                 for (int id = 1; id <= 10; id++)
17                 {
18                     customerTable.Rows.Add(
19                         new object[] { id, string.Format("customer{0}", id) });
20                 }
21                 //customerTable.AcceptChanges();
22 
23                 // Add another ten rows
24                 for (int id = 11; id <= 20; id++)
25                 {
26                     customerTable.Rows.Add(
27                         new object[] { id, string.Format("customer{0}", id) });
28                 }
29                 customerTable.AcceptChanges();
30 
31                 string expression;
32                 string sortOrder;
33 
34                 expression = "id > 5";
35                 // Sort descending by column named CompanyName.
36                 sortOrder = "name DESC";
37                 // Use the Select method to find all rows matching the filter.
38                 DataRow[] foundRows =
39                     customerTable.Select(expression, sortOrder,
40                     DataViewRowState.Added);
41 
42                 PrintRows(foundRows, "filtered rows");
43 
44                 foundRows = customerTable.Select();
45                 PrintRows(foundRows, "all rows");
46 
47                 Console.WriteLine("OK");
48             }
49             catch (Exception ex)
50             {
51                 Console.WriteLine(ex.Message);
52             }
53 
54             Console.ReadKey();
55         }
56         private static void PrintRows(DataRow[] rows, string label)
57         {
58             Console.WriteLine("
{0}", label);
59             if (rows.Length <= 0)
60             {
61                 Console.WriteLine("no rows found");
62                 return;
63             }
64             foreach (DataRow row in rows)
65             {
66                 foreach (DataColumn column in row.Table.Columns)
67                 {
68                     Console.Write("	 table {0}", row[column]);
69                 }
70                 Console.WriteLine();
71             }
72         }
View Code

 当使用 AcceptChanges 方法后,之前对datatable的 AddedModifiedUnchangedDeleted 都会被提交。比如下面这一行中 select 将检测不到 AcceptChanges之前的对表的增加。 

 DataRow[] foundRows =customerTable.Select(expression, sortOrder,DataViewRowState.Added);
原文地址:https://www.cnblogs.com/xieweikang/p/11661947.html