DataTable DataTable DataTable DataTable Class

定义

表示内存中数据的一个表。Represents one table of in-memory data.

public ref class DataTable : System::ComponentModel::MarshalByValueComponent, System::ComponentModel::IListSource, System::ComponentModel::ISupportInitializeNotification, System::Runtime::Serialization::ISerializable, System::Xml::Serialization::IXmlSerializable
[System.Serializable]
public class DataTable : System.ComponentModel.MarshalByValueComponent, System.ComponentModel.IListSource, System.ComponentModel.ISupportInitializeNotification, System.Runtime.Serialization.ISerializable, System.Xml.Serialization.IXmlSerializable
type DataTable = class
    inherit MarshalByValueComponent
    interface IListSource
    interface ISupportInitializeNotification
    interface ISerializable
    interface IXmlSerializable
    interface ISupportInitialize
Public Class DataTable
Inherits MarshalByValueComponent
Implements IListSource, ISerializable, ISupportInitializeNotification, IXmlSerializable
继承
派生
属性
实现

示例

下面的示例创建两DataTable个对象和DataRelation一个对象,并将新对象添加到DataSet中。The following example creates two DataTable objects and one DataRelation object, and adds the new objects to a DataSet. 然后,将在DataGridView控件中显示这些表。The tables are then displayed in a DataGridView control.

   // Put the next line into the Declarations section.
   private System.Data.DataSet dataSet;

   private void MakeDataTables()
   {
       // Run all of the functions. 
       MakeParentTable();
       MakeChildTable();
       MakeDataRelation();
       BindToDataGrid();
   }

   private void MakeParentTable()
   {
       // Create a new DataTable.
       System.Data.DataTable table = new DataTable("ParentTable");
       // Declare variables for DataColumn and DataRow objects.
       DataColumn column;
       DataRow row;

       // Create new DataColumn, set DataType, 
       // ColumnName and add to DataTable.    
       column = new DataColumn();
       column.DataType = System.Type.GetType("System.Int32");
       column.ColumnName = "id";
       column.ReadOnly = true;
       column.Unique = true;
       // Add the Column to the DataColumnCollection.
       table.Columns.Add(column);

       // Create second column.
       column = new DataColumn();
       column.DataType = System.Type.GetType("System.String");
       column.ColumnName = "ParentItem";
       column.AutoIncrement = false;
       column.Caption = "ParentItem";
       column.ReadOnly = false;
       column.Unique = false;
       // Add the column to the table.
       table.Columns.Add(column);

       // Make the ID column the primary key column.
       DataColumn[] PrimaryKeyColumns = new DataColumn[1];
       PrimaryKeyColumns[0] = table.Columns["id"];
       table.PrimaryKey = PrimaryKeyColumns;

       // Instantiate the DataSet variable.
       dataSet = new DataSet();
       // Add the new DataTable to the DataSet.
       dataSet.Tables.Add(table);

       // Create three new DataRow objects and add 
       // them to the DataTable
       for (int i = 0; i<= 2; i++)
       {
           row = table.NewRow();
           row["id"] = i;
           row["ParentItem"] = "ParentItem " + i;
           table.Rows.Add(row);
       }
   }

   private void MakeChildTable()
   {
       // Create a new DataTable.
       DataTable table = new DataTable("childTable");
       DataColumn column;
       DataRow row;

       // Create first column and add to the DataTable.
       column = new DataColumn();
       column.DataType= System.Type.GetType("System.Int32");
       column.ColumnName = "ChildID";
       column.AutoIncrement = true;
       column.Caption = "ID";
       column.ReadOnly = true;
       column.Unique = true;

       // Add the column to the DataColumnCollection.
       table.Columns.Add(column);

       // Create second column.
       column = new DataColumn();
       column.DataType= System.Type.GetType("System.String");
       column.ColumnName = "ChildItem";
       column.AutoIncrement = false;
       column.Caption = "ChildItem";
       column.ReadOnly = false;
       column.Unique = false;
       table.Columns.Add(column);

       // Create third column.
       column = new DataColumn();
       column.DataType= System.Type.GetType("System.Int32");
       column.ColumnName = "ParentID";
       column.AutoIncrement = false;
       column.Caption = "ParentID";
       column.ReadOnly = false;
       column.Unique = false;
       table.Columns.Add(column);

       dataSet.Tables.Add(table);

       // Create three sets of DataRow objects, 
       // five rows each, and add to DataTable.
       for(int i = 0; i <= 4; i ++)
       {
           row = table.NewRow();
           row["childID"] = i;
           row["ChildItem"] = "Item " + i;
           row["ParentID"] = 0 ;
           table.Rows.Add(row);
       }
       for(int i = 0; i <= 4; i ++)
       {
           row = table.NewRow();
           row["childID"] = i + 5;
           row["ChildItem"] = "Item " + i;
           row["ParentID"] = 1 ;
           table.Rows.Add(row);
       }
       for(int i = 0; i <= 4; i ++)
       {
           row = table.NewRow();
           row["childID"] = i + 10;
           row["ChildItem"] = "Item " + i;
           row["ParentID"] = 2 ;
           table.Rows.Add(row);
       }
   }

   private void MakeDataRelation()
   {
       // DataRelation requires two DataColumn 
       // (parent and child) and a name.
       DataColumn parentColumn = 
           dataSet.Tables["ParentTable"].Columns["id"];
       DataColumn childColumn = 
           dataSet.Tables["ChildTable"].Columns["ParentID"];
       DataRelation relation = new 
           DataRelation("parent2Child", parentColumn, childColumn);
       dataSet.Tables["ChildTable"].ParentRelations.Add(relation);
   }

   private void BindToDataGrid()
   {
       // Instruct the DataGrid to bind to the DataSet, with the 
       // ParentTable as the topmost DataTable.
       dataGrid1.SetDataBinding(dataSet,"ParentTable");
   }
' Put the next line into the Declarations section.
private dataSet As DataSet 
 
Private Sub MakeDataTables()
    ' Run all of the functions. 
    MakeParentTable()
    MakeChildTable()
    MakeDataRelation()
    BindToDataGrid()
End Sub
 
Private Sub MakeParentTable()
    ' Create a new DataTable.
    Dim table As New DataTable("ParentTable")

    ' Declare variables for DataColumn and DataRow objects.
    Dim column As DataColumn 
    Dim row As DataRow 
 
    ' Create new DataColumn, set DataType, ColumnName 
    ' and add to DataTable.    
    column = New DataColumn()
    column.DataType = System.Type.GetType("System.Int32")
    column.ColumnName = "id"
    column.ReadOnly = True
    column.Unique = True

    ' Add the Column to the DataColumnCollection.
    table.Columns.Add(column)
 
    ' Create second column.
    column = New DataColumn()
    column.DataType = System.Type.GetType("System.String")
    column.ColumnName = "ParentItem"
    column.AutoIncrement = False
    column.Caption = "ParentItem"
    column.ReadOnly = False
    column.Unique = False

    ' Add the column to the table.
    table.Columns.Add(column)
 
    ' Make the ID column the primary key column.
    Dim PrimaryKeyColumns(0) As DataColumn
    PrimaryKeyColumns(0)= table.Columns("id")
    table.PrimaryKey = PrimaryKeyColumns
 
    ' Instantiate the DataSet variable.
    dataSet = New DataSet()

    ' Add the new DataTable to the DataSet.
    dataSet.Tables.Add(table)
 
    ' Create three new DataRow objects and add 
    ' them to the DataTable
    Dim i As Integer
    For i = 0 to 2
       row = table.NewRow()
       row("id") = i
       row("ParentItem") = "ParentItem " + i.ToString()
       table.Rows.Add(row)
    Next i
End Sub
 
Private Sub MakeChildTable()
    ' Create a new DataTable.
    Dim table As New DataTable("childTable")
    Dim column As DataColumn 
    Dim row As DataRow 
 
    ' Create first column and add to the DataTable.
    column = New DataColumn()
    column.DataType= System.Type.GetType("System.Int32")
    column.ColumnName = "ChildID"
    column.AutoIncrement = True
    column.Caption = "ID"
    column.ReadOnly = True
    column.Unique = True

    ' Add the column to the DataColumnCollection.
    table.Columns.Add(column)
 
    ' Create second column.
    column = New DataColumn()
    column.DataType= System.Type.GetType("System.String")
    column.ColumnName = "ChildItem"
    column.AutoIncrement = False
    column.Caption = "ChildItem"
    column.ReadOnly = False
    column.Unique = False
    table.Columns.Add(column)
 
    ' Create third column.
    column = New DataColumn()
    column.DataType= System.Type.GetType("System.Int32")
    column.ColumnName = "ParentID"
    column.AutoIncrement = False
    column.Caption = "ParentID"
    column.ReadOnly = False
    column.Unique = False
    table.Columns.Add(column)
 
    dataSet.Tables.Add(table)

    ' Create three sets of DataRow objects, five rows each, 
    ' and add to DataTable.
    Dim i As Integer
    For i = 0 to 4
       row = table.NewRow()
       row("childID") = i
       row("ChildItem") = "Item " + i.ToString()
       row("ParentID") = 0 
       table.Rows.Add(row)
    Next i
    For i = 0 to 4
       row = table.NewRow()
       row("childID") = i + 5
       row("ChildItem") = "Item " + i.ToString()
       row("ParentID") = 1 
       table.Rows.Add(row)
    Next i
    For i = 0 to 4
       row = table.NewRow()
       row("childID") = i + 10
       row("ChildItem") = "Item " + i.ToString()
       row("ParentID") = 2 
       table.Rows.Add(row)
    Next i
End Sub
 
Private Sub MakeDataRelation()
    ' DataRelation requires two DataColumn 
    ' (parent and child) and a name.
    Dim parentColumn As DataColumn = _
        dataSet.Tables("ParentTable").Columns("id")
    Dim childColumn As DataColumn = _
        dataSet.Tables("ChildTable").Columns("ParentID")
    Dim relation As DataRelation = new _
        DataRelation("parent2Child", parentColumn, childColumn)
    dataSet.Tables("ChildTable").ParentRelations.Add(relation)
End Sub
 
Private Sub BindToDataGrid()
    ' Instruct the DataGrid to bind to the DataSet, with the 
    ' ParentTable as the topmost DataTable.
    DataGrid1.SetDataBinding(dataSet,"ParentTable")
End Sub
 

此示例演示如何使用特定的架构定义手动创建 DataTable:This sample demonstrates how to create a DataTable manually with specific schema definitions:

  • 创建多个 DataTable 并定义初始列。Create multiple DataTables and define the initial columns.

  • 创建表约束。Create the table constraints.

  • 插入值并显示表。Insert the values and display the tables.

  • 创建表达式列并显示表。Create the expression columns and display the tables.

C#可以在开发人员代码示例中找到带有此代码示例的 Visual Basic 项目。C# and Visual Basic projects with this code sample can be found on Developer Code Samples.

using System;
using System.Data;

class Program {
   static void Main(string[] args) {
      // Create two tables and add them into the DataSet
      DataTable orderTable = CreateOrderTable();
      DataTable orderDetailTable = CreateOrderDetailTable();
      DataSet salesSet = new DataSet();
      salesSet.Tables.Add(orderTable);
      salesSet.Tables.Add(orderDetailTable);

      // Set the relations between the tables and create the related constraint.
      salesSet.Relations.Add("OrderOrderDetail", orderTable.Columns["OrderId"], orderDetailTable.Columns["OrderId"], true);

      Console.WriteLine("After creating the foreign key constriant, you will see the following error if inserting order detail with the wrong OrderId: ");
      try {
         DataRow errorRow = orderDetailTable.NewRow();
         errorRow[0] = 1;
         errorRow[1] = "O0007";
         orderDetailTable.Rows.Add(errorRow);
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
      Console.WriteLine();

      // Insert the rows into the table
      InsertOrders(orderTable);
      InsertOrderDetails(orderDetailTable);

      Console.WriteLine("The initial Order table.");
      ShowTable(orderTable);

      Console.WriteLine("The OrderDetail table.");
      ShowTable(orderDetailTable);

      // Use the Aggregate-Sum on the child table column to get the result.
      DataColumn colSub = new DataColumn("SubTotal", typeof(Decimal), "Sum(Child.LineTotal)");
      orderTable.Columns.Add(colSub);

      // Compute the tax by referencing the SubTotal expression column.
      DataColumn colTax = new DataColumn("Tax", typeof(Decimal), "SubTotal*0.1");
      orderTable.Columns.Add(colTax);

      // If the OrderId is 'Total', compute the due on all orders; or compute the due on this order.
      DataColumn colTotal = new DataColumn("TotalDue", typeof(Decimal), "IIF(OrderId='Total',Sum(SubTotal)+Sum(Tax),SubTotal+Tax)");
      orderTable.Columns.Add(colTotal);

      DataRow row = orderTable.NewRow();
      row["OrderId"] = "Total";
      orderTable.Rows.Add(row);

      Console.WriteLine("The Order table with the expression columns.");
      ShowTable(orderTable);

      Console.WriteLine("Press any key to exit.....");
      Console.ReadKey();
   }

   private static DataTable CreateOrderTable() {
      DataTable orderTable = new DataTable("Order");

      // Define one column.
      DataColumn colId = new DataColumn("OrderId", typeof(String));
      orderTable.Columns.Add(colId);

      DataColumn colDate = new DataColumn("OrderDate", typeof(DateTime));
      orderTable.Columns.Add(colDate);

      // Set the OrderId column as the primary key.
      orderTable.PrimaryKey = new DataColumn[] { colId };

      return orderTable;
   }

   private static DataTable CreateOrderDetailTable() {
      DataTable orderDetailTable = new DataTable("OrderDetail");

      // Define all the columns once.
      DataColumn[] cols ={
                                  new DataColumn("OrderDetailId",typeof(Int32)),
                                  new DataColumn("OrderId",typeof(String)),
                                  new DataColumn("Product",typeof(String)),
                                  new DataColumn("UnitPrice",typeof(Decimal)),
                                  new DataColumn("OrderQty",typeof(Int32)),
                                  new DataColumn("LineTotal",typeof(Decimal),"UnitPrice*OrderQty")
                              };

      orderDetailTable.Columns.AddRange(cols);
      orderDetailTable.PrimaryKey = new DataColumn[] { orderDetailTable.Columns["OrderDetailId"] };
     return orderDetailTable;
   }

   private static void InsertOrders(DataTable orderTable) {
      // Add one row once.
      DataRow row1 = orderTable.NewRow();
      row1["OrderId"] = "O0001";
      row1["OrderDate"] = new DateTime(2013, 3, 1);
      orderTable.Rows.Add(row1);

      DataRow row2 = orderTable.NewRow();
      row2["OrderId"] = "O0002";
      row2["OrderDate"] = new DateTime(2013, 3, 12);
      orderTable.Rows.Add(row2);

      DataRow row3 = orderTable.NewRow();
      row3["OrderId"] = "O0003";
      row3["OrderDate"] = new DateTime(2013, 3, 20);
      orderTable.Rows.Add(row3);
   }

   private static void InsertOrderDetails(DataTable orderDetailTable) {
      // Use an Object array to insert all the rows .
      // Values in the array are matched sequentially to the columns, based on the order in which they appear in the table.
      Object[] rows = {
                                 new Object[]{1,"O0001","Mountain Bike",1419.5,36},
                                 new Object[]{2,"O0001","Road Bike",1233.6,16},
                                 new Object[]{3,"O0001","Touring Bike",1653.3,32},
                                 new Object[]{4,"O0002","Mountain Bike",1419.5,24},
                                 new Object[]{5,"O0002","Road Bike",1233.6,12},
                                 new Object[]{6,"O0003","Mountain Bike",1419.5,48},
                                 new Object[]{7,"O0003","Touring Bike",1653.3,8},
                             };

      foreach (Object[] row in rows) {
         orderDetailTable.Rows.Add(row);
      }
   }

   private static void ShowTable(DataTable table) {
      foreach (DataColumn col in table.Columns) {
         Console.Write("{0,-14}", col.ColumnName);
      }
      Console.WriteLine();

      foreach (DataRow row in table.Rows) {
         foreach (DataColumn col in table.Columns) {
            if (col.DataType.Equals(typeof(DateTime)))
               Console.Write("{0,-14:d}", row[col]);
            else if (col.DataType.Equals(typeof(Decimal)))
               Console.Write("{0,-14:C}", row[col]);
            else
               Console.Write("{0,-14}", row[col]);           
         }
         Console.WriteLine();
      }
      Console.WriteLine();
   }
}
Imports System.Data

Class Program
   Public Shared Sub Main(args As String())
      ' Create two tables and add them into the DataSet
      Dim orderTable As DataTable = CreateOrderTable()
      Dim orderDetailTable As DataTable = CreateOrderDetailTable()
      Dim salesSet As New DataSet()
      salesSet.Tables.Add(orderTable)
      salesSet.Tables.Add(orderDetailTable)

      ' Set the relations between the tables and create the related constraint.
      salesSet.Relations.Add("OrderOrderDetail", orderTable.Columns("OrderId"), orderDetailTable.Columns("OrderId"), True)

      Console.WriteLine("After creating the foreign key constriant, you will see the following error if inserting order detail with the wrong OrderId: ")
      Try
         Dim errorRow As DataRow = orderDetailTable.NewRow()
         errorRow(0) = 1
         errorRow(1) = "O0007"
         orderDetailTable.Rows.Add(errorRow)
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
      Console.WriteLine()

      ' Insert the rows into the table
      InsertOrders(orderTable)
      InsertOrderDetails(orderDetailTable)

      Console.WriteLine("The initial Order table.")
      ShowTable(orderTable)

      Console.WriteLine("The OrderDetail table.")
      ShowTable(orderDetailTable)

      ' Use the Aggregate-Sum on the child table column to get the result.
      Dim colSub As New DataColumn("SubTotal", GetType([Decimal]), "Sum(Child.LineTotal)")
      orderTable.Columns.Add(colSub)

      ' Compute the tax by referencing the SubTotal expression column.
      Dim colTax As New DataColumn("Tax", GetType([Decimal]), "SubTotal*0.1")
      orderTable.Columns.Add(colTax)

      ' If the OrderId is 'Total', compute the due on all orders; or compute the due on this order.
      Dim colTotal As New DataColumn("TotalDue", GetType([Decimal]), "IIF(OrderId='Total',Sum(SubTotal)+Sum(Tax),SubTotal+Tax)")
      orderTable.Columns.Add(colTotal)

      Dim row As DataRow = orderTable.NewRow()
      row("OrderId") = "Total"
      orderTable.Rows.Add(row)

      Console.WriteLine("The Order table with the expression columns.")
      ShowTable(orderTable)

      Console.WriteLine("Press any key to exit.....")
      Console.ReadKey()
   End Sub

   Private Shared Function CreateOrderTable() As DataTable
      Dim orderTable As New DataTable("Order")

      ' Define one column.
      Dim colId As New DataColumn("OrderId", GetType([String]))
      orderTable.Columns.Add(colId)

      Dim colDate As New DataColumn("OrderDate", GetType(DateTime))
      orderTable.Columns.Add(colDate)

      ' Set the OrderId column as the primary key.
      orderTable.PrimaryKey = New DataColumn() {colId}

      Return orderTable
   End Function

   Private Shared Function CreateOrderDetailTable() As DataTable
      Dim orderDetailTable As New DataTable("OrderDetail")

      ' Define all the columns once.
      Dim cols As DataColumn() = {New DataColumn("OrderDetailId", GetType(Int32)), New DataColumn("OrderId", GetType([String])), New DataColumn("Product", GetType([String])), New DataColumn("UnitPrice", GetType([Decimal])), New DataColumn("OrderQty", GetType(Int32)), New DataColumn("LineTotal", GetType([Decimal]), "UnitPrice*OrderQty")}

      orderDetailTable.Columns.AddRange(cols)
      orderDetailTable.PrimaryKey = New DataColumn() {orderDetailTable.Columns("OrderDetailId")}
      Return orderDetailTable
   End Function

   Private Shared Sub InsertOrders(orderTable As DataTable)
      ' Add one row once.
      Dim row1 As DataRow = orderTable.NewRow()
      row1("OrderId") = "O0001"
      row1("OrderDate") = New DateTime(2013, 3, 1)
      orderTable.Rows.Add(row1)

      Dim row2 As DataRow = orderTable.NewRow()
      row2("OrderId") = "O0002"
      row2("OrderDate") = New DateTime(2013, 3, 12)
      orderTable.Rows.Add(row2)

      Dim row3 As DataRow = orderTable.NewRow()
      row3("OrderId") = "O0003"
      row3("OrderDate") = New DateTime(2013, 3, 20)
      orderTable.Rows.Add(row3)
   End Sub

   Private Shared Sub InsertOrderDetails(orderDetailTable As DataTable)
      ' Use an Object array to insert all the rows .
      ' Values in the array are matched sequentially to the columns, based on the order in which they appear in the table.
      Dim rows As [Object]() = {New [Object]() {1, "O0001", "Mountain Bike", 1419.5, 36}, New [Object]() {2, "O0001", "Road Bike", 1233.6, 16}, New [Object]() {3, "O0001", "Touring Bike", 1653.3, 32}, New [Object]() {4, "O0002", "Mountain Bike", 1419.5, 24}, New [Object]() {5, "O0002", "Road Bike", 1233.6, 12}, New [Object]() {6, "O0003", "Mountain Bike", 1419.5, 48}, _
         New [Object]() {7, "O0003", "Touring Bike", 1653.3, 8}}

      For Each row As [Object]() In rows
         orderDetailTable.Rows.Add(row)
      Next
   End Sub

   Private Shared Sub ShowTable(table As DataTable)
      For Each col As DataColumn In table.Columns
         Console.Write("{0,-14}", col.ColumnName)
      Next
      Console.WriteLine()

      For Each row As DataRow In table.Rows
         For Each col As DataColumn In table.Columns
            If col.DataType.Equals(GetType(DateTime)) Then
               Console.Write("{0,-14:d}", row(col))
            ElseIf col.DataType.Equals(GetType([Decimal])) Then
               Console.Write("{0,-14:C}", row(col))
            Else
               Console.Write("{0,-14}", row(col))
            End If
         Next
         Console.WriteLine()
      Next
      Console.WriteLine()
   End Sub
End Class

注解

DataTable是 ADO.NET 库中的中心对象。The DataTable is a central object in the ADO.NET library. 使用的DataTable其他对象DataSet包括和DataViewOther objects that use the DataTable include the DataSet and the DataView.

在访问DataTable对象时,请注意,它们具有条件区分大小写。When accessing DataTable objects, note that they are conditionally case sensitive. 例如,如果一个DataTable名为 "mydatatable",另一个名为 "mydatatable",则用于搜索其中一个表的字符串被视为区分大小写。For example, if one DataTable is named "mydatatable" and another is named "Mydatatable", a string used to search for one of the tables is regarded as case sensitive. 但是,如果 "mydatatable" 存在并且 "Mydatatable" 不是,则搜索字符串将被视为不区分大小写。However, if "mydatatable" exists and "Mydatatable" does not, the search string is regarded as case insensitive. 可以包含两个DataTable对象,这些对象具有TableName相同的属性值,但属性值不同NamespaceDataSetA DataSet can contain two DataTable objects that have the same TableName property value but different Namespace property values. 有关使用DataTable对象的详细信息,请参阅创建 DataTableFor more information about working with DataTable objects, see Creating a DataTable.

如果要DataTable以编程方式创建,则必须先通过将对象添加DataColumn到来DataColumnCollection定义其架构(通过Columns属性访问)。If you are creating a DataTable programmatically, you must first define its schema by adding DataColumn objects to the DataColumnCollection (accessed through the Columns property). 有关添加DataColumn对象的详细信息,请参阅将列添加到 DataTableFor more information about adding DataColumn objects, see Adding Columns to a DataTable.

若要将行添加DataTable到中,必须首先NewRow使用方法返回新DataRow的对象。To add rows to a DataTable, you must first use the NewRow method to return a new DataRow object. 方法返回一个具有架构DataTable的行,因为它是由表的DataColumnCollection定义的。 NewRowThe NewRow method returns a row with the schema of the DataTable, as it is defined by the table's DataColumnCollection. DataTable可存储的最大行数为16777216。The maximum number of rows that a DataTable can store is 16,777,216. 有关详细信息,请参阅将数据添加到 DataTableFor more information, see Adding Data to a DataTable.

还包含一个Constraint对象集合,这些对象可用于确保数据的完整性。 DataTableThe DataTable also contains a collection of Constraint objects that can be used to ensure the integrity of the data. 有关详细信息,请参阅DataTable 约束For more information, see DataTable Constraints.

可以使用多DataTable个事件来确定何时对表进行了更改。There are many DataTable events that can be used to determine when changes are made to a table. 其中包括 RowChangedRowChangingRowDeletingRowDeletedThese include RowChanged, RowChanging, RowDeleting, and RowDeleted. 有关可与DataTable一起使用的事件的详细信息,请参阅处理 DataTable 事件For more information about the events that can be used with a DataTable, see Handling DataTable Events.

创建的实例DataTable后,某些读/写属性将设置为初始值。When an instance of DataTable is created, some of the read/write properties are set to initial values. 有关这些值的列表,请参阅DataTable.DataTable构造函数主题。For a list of these values, see the DataTable.DataTable constructor topic.

备注

和对象从ISerializable继承,并支持 .NET Framework 远程处理的接口。 MarshalByValueComponent DataTable DataSetThe DataSet and DataTable objects inherit from MarshalByValueComponent, and support the ISerializable interface for .NET Framework remoting. 这些是唯一可用于 .NET Framework 远程处理的 ADO.NET 对象。These are the only ADO.NET objects that you can use for .NET Framework remoting.

构造函数

DataTable() DataTable() DataTable() DataTable()

在不使用参数的情况下初始化 DataTable 类的新实例。Initializes a new instance of the DataTable class with no arguments.

DataTable(SerializationInfo, StreamingContext) DataTable(SerializationInfo, StreamingContext) DataTable(SerializationInfo, StreamingContext) DataTable(SerializationInfo, StreamingContext)

使用 SerializationInfoStreamingContext 初始化 DataTable 类的新实例。Initializes a new instance of the DataTable class with the SerializationInfo and the StreamingContext.

DataTable(String) DataTable(String) DataTable(String) DataTable(String)

使用指定的表名初始化 DataTable 类的新实例。Initializes a new instance of the DataTable class with the specified table name.

DataTable(String, String) DataTable(String, String) DataTable(String, String) DataTable(String, String)

使用指定的表名和命名空间初始化 DataTable 类的新实例。Initializes a new instance of the DataTable class using the specified table name and namespace.

字段

fInitInProgress fInitInProgress fInitInProgress fInitInProgress

检查是否正在进行初始化。Checks whether initialization is in progress. 初始化发生在运行时。The initialization occurs at run time.

属性

CaseSensitive CaseSensitive CaseSensitive CaseSensitive

指示表中的字符串比较是否区分大小写。Indicates whether string comparisons within the table are case-sensitive.

ChildRelations ChildRelations ChildRelations ChildRelations

获取此 DataTable 的子关系的集合。Gets the collection of child relations for this DataTable.

Columns Columns Columns Columns

获取属于该表的列的集合。Gets the collection of columns that belong to this table.

Constraints Constraints Constraints Constraints

获取由该表维护的约束的集合。Gets the collection of constraints maintained by this table.

Container Container Container Container

获取组件的容器。Gets the container for the component.

(Inherited from MarshalByValueComponent)
DataSet DataSet DataSet DataSet

获取此表所属的 DataSetGets the DataSet to which this table belongs.

DefaultView DefaultView DefaultView DefaultView

获取可能包含筛选视图或游标位置的表的自定义视图。Gets a customized view of the table that may include a filtered view, or a cursor position.

DesignMode DesignMode DesignMode DesignMode

获取指示组件当前是否处于设计模式的值。Gets a value indicating whether the component is currently in design mode.

(Inherited from MarshalByValueComponent)
DisplayExpression DisplayExpression DisplayExpression DisplayExpression

获取或设置一个表达式,该表达式返回的值用于在用户界面中表示此表。Gets or sets the expression that returns a value used to represent this table in the user interface. DisplayExpression 属性用于在用户界面中显示此表名。The DisplayExpression property lets you display the name of this table in a user interface.

Events Events Events Events

获取附加到该组件的事件处理程序的列表。Gets the list of event handlers that are attached to this component.

(Inherited from MarshalByValueComponent)
ExtendedProperties ExtendedProperties ExtendedProperties ExtendedProperties

获取自定义用户信息的集合。Gets the collection of customized user information.

HasErrors HasErrors HasErrors HasErrors

获取一个值,该值指示该表所属的 DataSet 的任何表的任何行中是否有错误。Gets a value indicating whether there are errors in any of the rows in any of the tables of the DataSet to which the table belongs.

IsInitialized IsInitialized IsInitialized IsInitialized

获取一个值,该值指示是否已初始化 DataTableGets a value that indicates whether the DataTable is initialized.

Locale Locale Locale Locale

获取或设置用于比较表中字符串的区域设置信息。Gets or sets the locale information used to compare strings within the table.

MinimumCapacity MinimumCapacity MinimumCapacity MinimumCapacity

获取或设置该表最初的起始大小。Gets or sets the initial starting size for this table.

Namespace Namespace Namespace Namespace

获取或设置 DataTable 中所存储数据的 XML 表示形式的命名空间。Gets or sets the namespace for the XML representation of the data stored in the DataTable.

ParentRelations ParentRelations ParentRelations ParentRelations

获取该 DataTable 的父关系的集合。Gets the collection of parent relations for this DataTable.

Prefix Prefix Prefix Prefix

获取或设置 DataTable 中所存储数据的 XML 表示形式的命名空间。Gets or sets the namespace for the XML representation of the data stored in the DataTable.

PrimaryKey PrimaryKey PrimaryKey PrimaryKey

获取或设置用作数据表主键的列数组。Gets or sets an array of columns that function as primary keys for the data table.

RemotingFormat RemotingFormat RemotingFormat RemotingFormat

获取或设置序列化格式。Gets or sets the serialization format.

Rows Rows Rows Rows

获取属于该表的行的集合。Gets the collection of rows that belong to this table.

Site Site Site Site

获取或设置 DataTableISiteGets or sets an ISite for the DataTable.

TableName TableName TableName TableName

获取或设置 DataTable 的名称。Gets or sets the name of the DataTable.

方法

AcceptChanges() AcceptChanges() AcceptChanges() AcceptChanges()

提交自上次调用 AcceptChanges() 以来对该表进行的所有更改。Commits all the changes made to this table since the last time AcceptChanges() was called.

BeginInit() BeginInit() BeginInit() BeginInit()

开始初始化在窗体上使用或由另一个组件使用的 DataTableBegins the initialization of a DataTable that is used on a form or used by another component. 初始化发生在运行时。The initialization occurs at run time.

BeginLoadData() BeginLoadData() BeginLoadData() BeginLoadData()

加载数据时,关闭通知、索引维护和约束。Turns off notifications, index maintenance, and constraints while loading data.

Clear() Clear() Clear() Clear()

清除所有数据的 DataTableClears the DataTable of all data.

Clone() Clone() Clone() Clone()

克隆 DataTable 的结构,包括所有 DataTable 架构和约束。Clones the structure of the DataTable, including all DataTable schemas and constraints.

Compute(String, String) Compute(String, String) Compute(String, String) Compute(String, String)

计算用来传递筛选条件的当前行上的给定表达式。Computes the given expression on the current rows that pass the filter criteria.

Copy() Copy() Copy() Copy()

复制该 DataTable 的结构和数据。Copies both the structure and data for this DataTable.

CreateDataReader() CreateDataReader() CreateDataReader() CreateDataReader()

返回与此 DataTable 内的数据对应的 DataTableReaderReturns a DataTableReader corresponding to the data within this DataTable.

CreateInstance() CreateInstance() CreateInstance() CreateInstance()

创建 DataTable 的新实例。Creates a new instance of DataTable.

Dispose() Dispose() Dispose() Dispose()

释放由 MarshalByValueComponent 使用的所有资源。Releases all resources used by the MarshalByValueComponent.

(Inherited from MarshalByValueComponent)
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

释放由 MarshalByValueComponent 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the MarshalByValueComponent and optionally releases the managed resources.

(Inherited from MarshalByValueComponent)
EndInit() EndInit() EndInit() EndInit()

结束在窗体上使用或由另一个组件使用的 DataTable 的初始化。Ends the initialization of a DataTable that is used on a form or used by another component. 初始化发生在运行时。The initialization occurs at run time.

EndLoadData() EndLoadData() EndLoadData() EndLoadData()

加载数据后,打开通知、索引维护和约束。Turns on notifications, index maintenance, and constraints after loading data.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetChanges() GetChanges() GetChanges() GetChanges()

获取 DataTable 的副本,该副本包含自加载以来或自上次调用 AcceptChanges() 以来进行的所有更改。Gets a copy of the DataTable that contains all changes made to it since it was loaded or AcceptChanges() was last called.

GetChanges(DataRowState) GetChanges(DataRowState) GetChanges(DataRowState) GetChanges(DataRowState)

获取由 DataRowState 筛选的 DataTable 的副本,该副本包含上次加载以来或调用 AcceptChanges() 以来进行的所有更改。Gets a copy of the DataTable containing all changes made to it since it was last loaded, or since AcceptChanges() was called, filtered by DataRowState.

GetDataTableSchema(XmlSchemaSet) GetDataTableSchema(XmlSchemaSet) GetDataTableSchema(XmlSchemaSet) GetDataTableSchema(XmlSchemaSet)

该方法返回一个包含 Web Services 描述语言 (WSDL) 的 XmlSchemaSet 实例,该语言描述了用于 Web 服务的 DataTableThis method returns an XmlSchemaSet instance containing the Web Services Description Language (WSDL) that describes the DataTable for Web Services.

GetErrors() GetErrors() GetErrors() GetErrors()

获取包含错误的 DataRow 对象数组。Gets an array of DataRow objects that contain errors.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

使用序列化 DataTable 时所需的数据填充序列化信息对象。Populates a serialization information object with the data needed to serialize the DataTable.

GetRowType() GetRowType() GetRowType() GetRowType()

获取行类型。Gets the row type.

GetSchema() GetSchema() GetSchema() GetSchema()

有关此成员的说明,请参见 GetSchema()For a description of this member, see GetSchema().

GetService(Type) GetService(Type) GetService(Type) GetService(Type)

获取 IServiceProvider 的实施者。Gets the implementer of the IServiceProvider.

(Inherited from MarshalByValueComponent)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
ImportRow(DataRow) ImportRow(DataRow) ImportRow(DataRow) ImportRow(DataRow)

DataRow 复制到 DataTable 中,保留任何属性设置以及初始值和当前值。Copies a DataRow into a DataTable, preserving any property settings, as well as original and current values.

Load(IDataReader) Load(IDataReader) Load(IDataReader) Load(IDataReader)

通过所提供的 IDataReader,用某个数据源的值填充 DataTableFills a DataTable with values from a data source using the supplied IDataReader. 如果 DataTable 已经包含行,则从数据源传入的数据与现有行合并。If the DataTable already contains rows, the incoming data from the data source is merged with the existing rows.

Load(IDataReader, LoadOption) Load(IDataReader, LoadOption) Load(IDataReader, LoadOption) Load(IDataReader, LoadOption)

通过所提供的 IDataReader,用某个数据源的值填充 DataTableFills a DataTable with values from a data source using the supplied IDataReader. 如果 DataTable 已包含行,则从数据源传入的数据根据 loadOption 参数的值与现有行合并。If the DataTable already contains rows, the incoming data from the data source is merged with the existing rows according to the value of the loadOption parameter.

Load(IDataReader, LoadOption, FillErrorEventHandler) Load(IDataReader, LoadOption, FillErrorEventHandler) Load(IDataReader, LoadOption, FillErrorEventHandler) Load(IDataReader, LoadOption, FillErrorEventHandler)

通过所提供的使用错误处理委托的 IDataReader,用某个数据源中的值填充 DataTableFills a DataTable with values from a data source using the supplied IDataReader using an error-handling delegate.

LoadDataRow(Object[], Boolean) LoadDataRow(Object[], Boolean) LoadDataRow(Object[], Boolean) LoadDataRow(Object[], Boolean)

查找和更新特定行。Finds and updates a specific row. 如果找不到任何匹配行,则使用给定值创建新行。If no matching row is found, a new row is created using the given values.

LoadDataRow(Object[], LoadOption) LoadDataRow(Object[], LoadOption) LoadDataRow(Object[], LoadOption) LoadDataRow(Object[], LoadOption)

查找和更新特定行。Finds and updates a specific row. 如果找不到任何匹配行,则使用给定值创建新行。If no matching row is found, a new row is created using the given values.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Merge(DataTable) Merge(DataTable) Merge(DataTable) Merge(DataTable)

将指定的 DataTable 与当前 DataTable 合并。Merge the specified DataTable with the current DataTable.

Merge(DataTable, Boolean) Merge(DataTable, Boolean) Merge(DataTable, Boolean) Merge(DataTable, Boolean)

将指定的 DataTable 与当前 DataTable 合并,指示是否保留当前 DataTable 中的更改。Merge the specified DataTable with the current DataTable, indicating whether to preserve changes in the current DataTable.

Merge(DataTable, Boolean, MissingSchemaAction) Merge(DataTable, Boolean, MissingSchemaAction) Merge(DataTable, Boolean, MissingSchemaAction) Merge(DataTable, Boolean, MissingSchemaAction)

将指定的 DataTable 与当前 DataTable 合并,指示是否保留更改以及如何处理当前 DataTable 中缺失的架构。Merge the specified DataTable with the current DataTable, indicating whether to preserve changes and how to handle missing schema in the current DataTable.

NewRow() NewRow() NewRow() NewRow()

创建与该表具有相同架构的新 DataRowCreates a new DataRow with the same schema as the table.

NewRowArray(Int32) NewRowArray(Int32) NewRowArray(Int32) NewRowArray(Int32)

返回 DataRow 的数组。Returns an array of DataRow.

NewRowFromBuilder(DataRowBuilder) NewRowFromBuilder(DataRowBuilder) NewRowFromBuilder(DataRowBuilder) NewRowFromBuilder(DataRowBuilder)

从现有行创建新行。Creates a new row from an existing row.

OnColumnChanged(DataColumnChangeEventArgs) OnColumnChanged(DataColumnChangeEventArgs) OnColumnChanged(DataColumnChangeEventArgs) OnColumnChanged(DataColumnChangeEventArgs)

引发 ColumnChanged 事件。Raises the ColumnChanged event.

OnColumnChanging(DataColumnChangeEventArgs) OnColumnChanging(DataColumnChangeEventArgs) OnColumnChanging(DataColumnChangeEventArgs) OnColumnChanging(DataColumnChangeEventArgs)

引发 ColumnChanging 事件。Raises the ColumnChanging event.

OnPropertyChanging(PropertyChangedEventArgs) OnPropertyChanging(PropertyChangedEventArgs) OnPropertyChanging(PropertyChangedEventArgs) OnPropertyChanging(PropertyChangedEventArgs)

引发 PropertyChanged 事件。Raises the PropertyChanged event.

OnRemoveColumn(DataColumn) OnRemoveColumn(DataColumn) OnRemoveColumn(DataColumn) OnRemoveColumn(DataColumn)

通知 DataTable 正在移除 DataColumnNotifies the DataTable that a DataColumn is being removed.

OnRowChanged(DataRowChangeEventArgs) OnRowChanged(DataRowChangeEventArgs) OnRowChanged(DataRowChangeEventArgs) OnRowChanged(DataRowChangeEventArgs)

引发 RowChanged 事件。Raises the RowChanged event.

OnRowChanging(DataRowChangeEventArgs) OnRowChanging(DataRowChangeEventArgs) OnRowChanging(DataRowChangeEventArgs) OnRowChanging(DataRowChangeEventArgs)

引发 RowChanging 事件。Raises the RowChanging event.

OnRowDeleted(DataRowChangeEventArgs) OnRowDeleted(DataRowChangeEventArgs) OnRowDeleted(DataRowChangeEventArgs) OnRowDeleted(DataRowChangeEventArgs)

引发 RowDeleted 事件。Raises the RowDeleted event.

OnRowDeleting(DataRowChangeEventArgs) OnRowDeleting(DataRowChangeEventArgs) OnRowDeleting(DataRowChangeEventArgs) OnRowDeleting(DataRowChangeEventArgs)

引发 RowDeleting 事件。Raises the RowDeleting event.

OnTableCleared(DataTableClearEventArgs) OnTableCleared(DataTableClearEventArgs) OnTableCleared(DataTableClearEventArgs) OnTableCleared(DataTableClearEventArgs)

引发 TableCleared 事件。Raises the TableCleared event.

OnTableClearing(DataTableClearEventArgs) OnTableClearing(DataTableClearEventArgs) OnTableClearing(DataTableClearEventArgs) OnTableClearing(DataTableClearEventArgs)

引发 TableClearing 事件。Raises the TableClearing event.

OnTableNewRow(DataTableNewRowEventArgs) OnTableNewRow(DataTableNewRowEventArgs) OnTableNewRow(DataTableNewRowEventArgs) OnTableNewRow(DataTableNewRowEventArgs)

引发 TableNewRow 事件。Raises the TableNewRow event.

ReadXml(Stream) ReadXml(Stream) ReadXml(Stream) ReadXml(Stream)

使用指定的 Stream 将 XML 架构和数据读入 DataTableReads XML schema and data into the DataTable using the specified Stream.

ReadXml(String) ReadXml(String) ReadXml(String) ReadXml(String)

从指定的文件将 XML 架构和数据读入 DataTableReads XML schema and data into the DataTable from the specified file.

ReadXml(TextReader) ReadXml(TextReader) ReadXml(TextReader) ReadXml(TextReader)

使用指定的 TextReader 将 XML 架构和数据读入 DataTableReads XML schema and data into the DataTable using the specified TextReader.

ReadXml(XmlReader) ReadXml(XmlReader) ReadXml(XmlReader) ReadXml(XmlReader)

使用指定的 XmlReader 将 XML 架构和数据读入 DataTableReads XML Schema and Data into the DataTable using the specified XmlReader.

ReadXmlSchema(Stream) ReadXmlSchema(Stream) ReadXmlSchema(Stream) ReadXmlSchema(Stream)

使用指定的流将 XML 架构读入 DataTableReads an XML schema into the DataTable using the specified stream.

ReadXmlSchema(String) ReadXmlSchema(String) ReadXmlSchema(String) ReadXmlSchema(String)

从指定的文件将 XML 架构读入 DataTableReads an XML schema into the DataTable from the specified file.

ReadXmlSchema(TextReader) ReadXmlSchema(TextReader) ReadXmlSchema(TextReader) ReadXmlSchema(TextReader)

使用指定的 TextReader 将 XML 架构读入 DataTableReads an XML schema into the DataTable using the specified TextReader.

ReadXmlSchema(XmlReader) ReadXmlSchema(XmlReader) ReadXmlSchema(XmlReader) ReadXmlSchema(XmlReader)

使用指定的 XmlReader 将 XML 架构读入 DataTableReads an XML schema into the DataTable using the specified XmlReader.

ReadXmlSerializable(XmlReader) ReadXmlSerializable(XmlReader) ReadXmlSerializable(XmlReader) ReadXmlSerializable(XmlReader)

从 XML 流中读取。Reads from an XML stream.

RejectChanges() RejectChanges() RejectChanges() RejectChanges()

回滚自该表加载以来或上次调用 AcceptChanges() 以来对该表进行的所有更改。Rolls back all changes that have been made to the table since it was loaded, or the last time AcceptChanges() was called.

Reset() Reset() Reset() Reset()

DataTable 重置为其初始状态。Resets the DataTable to its original state. 重置将移除表的所有数据、索引、关系和列。Reset removes all data, indexes, relations, and columns of the table. 如果数据集包含一个数据表,则在重置该表之后,它将仍是数据集的一部分。If a DataSet includes a DataTable, the table will still be part of the DataSet after the table is reset.

Select() Select() Select() Select()

获取由所有 DataRow 对象组成的数组。Gets an array of all DataRow objects.

Select(String) Select(String) Select(String) Select(String)

获取由与筛选条件匹配的所有 DataRow 对象组成的数组。Gets an array of all DataRow objects that match the filter criteria.

Select(String, String) Select(String, String) Select(String, String) Select(String, String)

以指定排序顺序,获取由与筛选条件匹配的所有 DataRow 对象组成的数组。Gets an array of all DataRow objects that match the filter criteria, in the specified sort order.

Select(String, String, DataViewRowState) Select(String, String, DataViewRowState) Select(String, String, DataViewRowState) Select(String, String, DataViewRowState)

以与指定状态匹配的排序顺序,获取由与筛选条件匹配的所有 DataRow 对象组成的数组。Gets an array of all DataRow objects that match the filter in the order of the sort that match the specified state.

ToString() ToString() ToString() ToString()

获取 TableNameDisplayExpression(如果有一个用作连接字符串)。Gets the TableName and DisplayExpression, if there is one as a concatenated string.

WriteXml(Stream) WriteXml(Stream) WriteXml(Stream) WriteXml(Stream)

通过指定的 Stream,按 XML 形式编写 DataTable 的当前内容。Writes the current contents of the DataTable as XML using the specified Stream.

WriteXml(Stream, Boolean) WriteXml(Stream, Boolean) WriteXml(Stream, Boolean) WriteXml(Stream, Boolean)

通过指定的 Stream,按 XML 形式编写 DataTable 的当前内容。Writes the current contents of the DataTable as XML using the specified Stream. 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(Stream, XmlWriteMode) WriteXml(Stream, XmlWriteMode) WriteXml(Stream, XmlWriteMode) WriteXml(Stream, XmlWriteMode)

使用指定的 XmlWriteModeDataTable 的当前数据和架构(可选)写入指定的文件。Writes the current data, and optionally the schema, for the DataTable to the specified file using the specified XmlWriteMode. 若要写入架构,请将 mode 参数的值设置为 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema.

WriteXml(Stream, XmlWriteMode, Boolean) WriteXml(Stream, XmlWriteMode, Boolean) WriteXml(Stream, XmlWriteMode, Boolean) WriteXml(Stream, XmlWriteMode, Boolean)

使用指定的 XmlWriteModeDataTable 的当前数据和架构(可选)写入指定的文件。Writes the current data, and optionally the schema, for the DataTable to the specified file using the specified XmlWriteMode. 若要写入架构,请将 mode 参数的值设置为 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema. 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(String) WriteXml(String) WriteXml(String) WriteXml(String)

使用指定的文件以 XML 形式写入 DataTable 的当前内容。Writes the current contents of the DataTable as XML using the specified file.

WriteXml(String, Boolean) WriteXml(String, Boolean) WriteXml(String, Boolean) WriteXml(String, Boolean)

使用指定的文件以 XML 形式写入 DataTable 的当前内容。Writes the current contents of the DataTable as XML using the specified file. 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(String, XmlWriteMode) WriteXml(String, XmlWriteMode) WriteXml(String, XmlWriteMode) WriteXml(String, XmlWriteMode)

使用指定的文件和 XmlWriteMode 写入 DataTable 的当前数据和架构(可选)。Writes the current data, and optionally the schema, for the DataTable using the specified file and XmlWriteMode. 若要写入架构,请将 mode 参数的值设置为 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema.

WriteXml(String, XmlWriteMode, Boolean) WriteXml(String, XmlWriteMode, Boolean) WriteXml(String, XmlWriteMode, Boolean) WriteXml(String, XmlWriteMode, Boolean)

使用指定的文件和 XmlWriteMode 写入 DataTable 的当前数据和架构(可选)。Writes the current data, and optionally the schema, for the DataTable using the specified file and XmlWriteMode. 若要写入架构,请将 mode 参数的值设置为 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema. 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(TextWriter) WriteXml(TextWriter) WriteXml(TextWriter) WriteXml(TextWriter)

通过指定的 TextWriter,按 XML 形式编写 DataTable 的当前内容。Writes the current contents of the DataTable as XML using the specified TextWriter.

WriteXml(TextWriter, Boolean) WriteXml(TextWriter, Boolean) WriteXml(TextWriter, Boolean) WriteXml(TextWriter, Boolean)

通过指定的 TextWriter,按 XML 形式编写 DataTable 的当前内容。Writes the current contents of the DataTable as XML using the specified TextWriter. 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(TextWriter, XmlWriteMode) WriteXml(TextWriter, XmlWriteMode) WriteXml(TextWriter, XmlWriteMode) WriteXml(TextWriter, XmlWriteMode)

使用指定的 TextWriterXmlWriteMode 写入 DataTable 的当前数据和架构(可选)。Writes the current data, and optionally the schema, for the DataTable using the specified TextWriter and XmlWriteMode. 若要写入架构,请将 mode 参数的值设置为 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema.

WriteXml(TextWriter, XmlWriteMode, Boolean) WriteXml(TextWriter, XmlWriteMode, Boolean) WriteXml(TextWriter, XmlWriteMode, Boolean) WriteXml(TextWriter, XmlWriteMode, Boolean)

使用指定的 TextWriterXmlWriteMode 写入 DataTable 的当前数据和架构(可选)。Writes the current data, and optionally the schema, for the DataTable using the specified TextWriter and XmlWriteMode. 若要写入架构,请将 mode 参数的值设置为 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema. 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(XmlWriter) WriteXml(XmlWriter) WriteXml(XmlWriter) WriteXml(XmlWriter)

通过指定的 XmlWriter,按 XML 形式编写 DataTable 的当前内容。Writes the current contents of the DataTable as XML using the specified XmlWriter.

WriteXml(XmlWriter, Boolean) WriteXml(XmlWriter, Boolean) WriteXml(XmlWriter, Boolean) WriteXml(XmlWriter, Boolean)

通过指定的 XmlWriter,按 XML 形式编写 DataTable 的当前内容。Writes the current contents of the DataTable as XML using the specified XmlWriter.

WriteXml(XmlWriter, XmlWriteMode) WriteXml(XmlWriter, XmlWriteMode) WriteXml(XmlWriter, XmlWriteMode) WriteXml(XmlWriter, XmlWriteMode)

使用指定的 XmlWriterXmlWriteMode 写入 DataTable 的当前数据和架构(可选)。Writes the current data, and optionally the schema, for the DataTable using the specified XmlWriter and XmlWriteMode. 若要写入架构,请将 mode 参数的值设置为 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema.

WriteXml(XmlWriter, XmlWriteMode, Boolean) WriteXml(XmlWriter, XmlWriteMode, Boolean) WriteXml(XmlWriter, XmlWriteMode, Boolean) WriteXml(XmlWriter, XmlWriteMode, Boolean)

使用指定的 XmlWriterXmlWriteMode 写入 DataTable 的当前数据和架构(可选)。Writes the current data, and optionally the schema, for the DataTable using the specified XmlWriter and XmlWriteMode. 若要写入架构,请将 mode 参数的值设置为 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema. 若要保存该表及其所有子代的数据,请将 writeHierarchy 参数设置为 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXmlSchema(Stream) WriteXmlSchema(Stream) WriteXmlSchema(Stream) WriteXmlSchema(Stream)

以 XML 架构的形式将 DataTable 的当前数据结构写入指定的流。Writes the current data structure of the DataTable as an XML schema to the specified stream.

WriteXmlSchema(Stream, Boolean) WriteXmlSchema(Stream, Boolean) WriteXmlSchema(Stream, Boolean) WriteXmlSchema(Stream, Boolean)

以 XML 架构的形式将 DataTable 的当前数据结构写入指定的流。Writes the current data structure of the DataTable as an XML schema to the specified stream. 若要保存该表及其所有子代的架构,请将 writeHierarchy 参数设置为 trueTo save the schema for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXmlSchema(String) WriteXmlSchema(String) WriteXmlSchema(String) WriteXmlSchema(String)

以 XML 架构的形式将 DataTable 的当前数据结构写入指定的文件。Writes the current data structure of the DataTable as an XML schema to the specified file.

WriteXmlSchema(String, Boolean) WriteXmlSchema(String, Boolean) WriteXmlSchema(String, Boolean) WriteXmlSchema(String, Boolean)

以 XML 架构的形式将 DataTable 的当前数据结构写入指定的文件。Writes the current data structure of the DataTable as an XML schema to the specified file. 若要保存该表及其所有子代的架构,请将 writeHierarchy 参数设置为 trueTo save the schema for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXmlSchema(TextWriter) WriteXmlSchema(TextWriter) WriteXmlSchema(TextWriter) WriteXmlSchema(TextWriter)

使用指定的 TextWriter 以 XML 架构的形式写入 DataTable 的当前数据结构。Writes the current data structure of the DataTable as an XML schema using the specified TextWriter.

WriteXmlSchema(TextWriter, Boolean) WriteXmlSchema(TextWriter, Boolean) WriteXmlSchema(TextWriter, Boolean) WriteXmlSchema(TextWriter, Boolean)

使用指定的 TextWriter 以 XML 架构的形式写入 DataTable 的当前数据结构。Writes the current data structure of the DataTable as an XML schema using the specified TextWriter. 若要保存该表及其所有子代的架构,请将 writeHierarchy 参数设置为 trueTo save the schema for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXmlSchema(XmlWriter) WriteXmlSchema(XmlWriter) WriteXmlSchema(XmlWriter) WriteXmlSchema(XmlWriter)

使用指定的 XmlWriter 以 XML 架构的形式写入 DataTable 的当前数据结构。Writes the current data structure of the DataTable as an XML schema using the specified XmlWriter.

WriteXmlSchema(XmlWriter, Boolean) WriteXmlSchema(XmlWriter, Boolean) WriteXmlSchema(XmlWriter, Boolean) WriteXmlSchema(XmlWriter, Boolean)

使用指定的 XmlWriter 以 XML 架构的形式写入 DataTable 的当前数据结构。Writes the current data structure of the DataTable as an XML schema using the specified XmlWriter. 若要保存该表及其所有子代的架构,请将 writeHierarchy 参数设置为 trueTo save the schema for the table and all its descendants, set the writeHierarchy parameter to true.

事件

ColumnChanged ColumnChanged ColumnChanged ColumnChanged

更改 DataRow 中指定的 DataColumn 值之后发生。Occurs after a value has been changed for the specified DataColumn in a DataRow.

ColumnChanging ColumnChanging ColumnChanging ColumnChanging

更改 DataRow 中指定的 DataColumn 值时发生。Occurs when a value is being changed for the specified DataColumn in a DataRow.

Disposed Disposed Disposed Disposed

添加事件处理程序以侦听组件上的 Disposed 事件。Adds an event handler to listen to the Disposed event on the component.

(Inherited from MarshalByValueComponent)
Initialized Initialized Initialized Initialized

初始化 DataTable 后发生。Occurs after the DataTable is initialized.

RowChanged RowChanged RowChanged RowChanged

在成功更改 DataRow 后发生。Occurs after a DataRow has been changed successfully.

RowChanging RowChanging RowChanging RowChanging

在更改 DataRow 时发生。Occurs when a DataRow is changing.

RowDeleted RowDeleted RowDeleted RowDeleted

删除表中的行后发生。Occurs after a row in the table has been deleted.

RowDeleting RowDeleting RowDeleting RowDeleting

要删除表中的行之前发生。Occurs before a row in the table is about to be deleted.

TableCleared TableCleared TableCleared TableCleared

清除 DataTable 后发生。Occurs after a DataTable is cleared.

TableClearing TableClearing TableClearing TableClearing

清除 DataTable 时发生。Occurs when a DataTable is cleared.

TableNewRow TableNewRow TableNewRow TableNewRow

插入新的 DataRow 时发生。Occurs when a new DataRow is inserted.

显式界面实现

IListSource.ContainsListCollection IListSource.ContainsListCollection IListSource.ContainsListCollection IListSource.ContainsListCollection

有关此成员的说明,请参见 ContainsListCollectionFor a description of this member, see ContainsListCollection.

IListSource.GetList() IListSource.GetList() IListSource.GetList() IListSource.GetList()

有关此成员的说明,请参见 GetList()For a description of this member, see GetList().

ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext)

使用序列化 DataTable 时所需的数据填充序列化信息对象。Populates a serialization information object with the data needed to serialize the DataTable.

IXmlSerializable.GetSchema() IXmlSerializable.GetSchema() IXmlSerializable.GetSchema() IXmlSerializable.GetSchema()

有关此成员的说明,请参见 GetSchema()For a description of this member, see GetSchema().

IXmlSerializable.ReadXml(XmlReader) IXmlSerializable.ReadXml(XmlReader) IXmlSerializable.ReadXml(XmlReader) IXmlSerializable.ReadXml(XmlReader)

有关此成员的说明,请参见 ReadXml(XmlReader)For a description of this member, see ReadXml(XmlReader).

IXmlSerializable.WriteXml(XmlWriter) IXmlSerializable.WriteXml(XmlWriter) IXmlSerializable.WriteXml(XmlWriter) IXmlSerializable.WriteXml(XmlWriter)

有关此成员的说明,请参见 WriteXml(XmlWriter)For a description of this member, see WriteXml(XmlWriter).

扩展方法

AsDataView(DataTable) AsDataView(DataTable) AsDataView(DataTable) AsDataView(DataTable)

创建并返回支持 LINQ 的 DataView 对象。Creates and returns a LINQ-enabled DataView object.

AsEnumerable(DataTable) AsEnumerable(DataTable) AsEnumerable(DataTable) AsEnumerable(DataTable)

返回一个 IEnumerable<T> 对象,其泛型参数 TDataRowReturns an IEnumerable<T> object, where the generic parameter T is DataRow. 此对象可用在 LINQLINQ 表达式或方法查询中。This object can be used in a LINQLINQ expression or method query.

适用于

线程安全性

对于多线程读取操作,此类型是安全的。This type is safe for multithreaded read operations. 必须同步任何写入操作。You must synchronize any write operations.

另请参阅