DataTable 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表一個記憶體內資料表。
public ref class DataTable : System::ComponentModel::MarshalByValueComponent, System::ComponentModel::IListSource, System::ComponentModel::ISupportInitialize, System::Runtime::Serialization::ISerializable
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.ISupportInitialize, System.Runtime.Serialization.ISerializable
[System.Serializable]
public class DataTable : System.ComponentModel.MarshalByValueComponent, System.ComponentModel.IListSource, System.ComponentModel.ISupportInitializeNotification, System.Runtime.Serialization.ISerializable, System.Xml.Serialization.IXmlSerializable
public class DataTable : System.ComponentModel.MarshalByValueComponent, System.ComponentModel.IListSource, System.ComponentModel.ISupportInitializeNotification, System.Runtime.Serialization.ISerializable, System.Xml.Serialization.IXmlSerializable
[<System.Serializable>]
type DataTable = class
inherit MarshalByValueComponent
interface IListSource
interface ISupportInitialize
interface ISerializable
[<System.Serializable>]
type DataTable = class
inherit MarshalByValueComponent
interface IListSource
interface ISupportInitializeNotification
interface ISupportInitialize
interface ISerializable
interface IXmlSerializable
type DataTable = class
inherit MarshalByValueComponent
interface IListSource
interface ISupportInitialize
interface ISupportInitializeNotification
interface ISerializable
interface IXmlSerializable
Public Class DataTable
Inherits MarshalByValueComponent
Implements IListSource, ISerializable, ISupportInitialize
Public Class DataTable
Inherits MarshalByValueComponent
Implements IListSource, ISerializable, ISupportInitializeNotification, IXmlSerializable
- 繼承
- 衍生
- 屬性
- 實作
範例
以下範例會產生兩個 DataTable 物件和一個 DataRelation 物件,並將新的物件加入一個 DataSet。 這些表格會以控制方式顯示 DataGridView 。
// 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
備註
欲了解更多關於此 API 的資訊,請參閱 DataTable 的補充 API 備註。
建構函式
| 名稱 | Description |
|---|---|
| DataTable() |
初始化一個無參數的新類別實例 DataTable 。 |
| DataTable(SerializationInfo, StreamingContext) |
使用串行化數據,初始化 DataTable 類別的新實例。 |
| DataTable(String, String) |
使用指定的資料表名稱與命名空間初始化該類別的新實例 DataTable 。 |
| DataTable(String) |
初始化一個以指定資料表名稱的新 DataTable 類別實例。 |
欄位
| 名稱 | Description |
|---|---|
| fInitInProgress |
檢查初始化是否正在進行中。 初始化發生在執行時。 |
屬性
| 名稱 | Description |
|---|---|
| CaseSensitive |
表示表中字串比較是否區分大小寫。 |
| ChildRelations |
為此 DataTable收集兒童關係。 |
| Columns |
取得屬於此表的欄位集合。 |
| Constraints |
取得由此表維持的約束集合。 |
| Container |
取得元件的容器。 (繼承來源 MarshalByValueComponent) |
| DataSet |
這 DataSet 張桌子該屬於什麼。 |
| DefaultView |
會獲得自訂的表格視圖,可能包含過濾檢視或游標位置。 |
| DesignMode |
會取得一個值,表示該元件目前是否處於設計模式。 (繼承來源 MarshalByValueComponent) |
| DisplayExpression |
取得或設定一個表達式,回傳用來在使用者介面中表示此表格的值。 這個 |
| Events |
取得與此元件相連的事件處理程序清單。 (繼承來源 MarshalByValueComponent) |
| ExtendedProperties |
取得自訂用戶資訊的集合。 |
| HasErrors |
會得到一個值,表示該資料表所屬的任一資料表 DataSet 中是否有錯誤。 |
| IsInitialized |
會得到一個表示是否 DataTable 已初始化的值。 |
| Locale |
取得或設定用於比較表格中字串的地點資訊。 |
| MinimumCapacity |
取得或設定此表的初始大小。 |
| Namespace |
取得或設定儲存在 DataTable. 中資料的 XML 表示名稱空間。 |
| ParentRelations |
取得父 DataTable關係的集合。 |
| Prefix |
取得或設定儲存在 DataTable. 中資料的 XML 表示名稱空間。 |
| PrimaryKey |
取得或設定一個欄位陣列,作為資料表的主鍵。 |
| RemotingFormat |
取得或設定序列化格式。 |
| Rows |
取得屬於此表的列集合。 |
| Site | |
| TableName |
取得或設定 的名稱。DataTable |
方法
事件
| 名稱 | Description |
|---|---|
| ColumnChanged |
發生在 中指定DataColumnDataRow值被更改後。 |
| ColumnChanging |
當值在 中被更改為指定的DataColumnDataRow值時,會發生。 |
| Disposed |
新增一個事件處理器來監聽元件上的事件。Disposed (繼承來源 MarshalByValueComponent) |
| Initialized |
發生在初始 DataTable 化之後。 |
| RowChanged |
發生在 a DataRow 成功更改之後。 |
| RowChanging |
當 a DataRow 正在改變時會發生。 |
| RowDeleted |
發生在資料表中某一列被刪除之後。 |
| RowDeleting |
發生在資料表中某一列即將被刪除之前。 |
| TableCleared |
在A DataTable 被清除後發生。 |
| TableClearing |
當 a DataTable 被清除時發生。 |
| TableNewRow |
當新插入 DataRow 時會發生。 |
明確介面實作
| 名稱 | Description |
|---|---|
| IListSource.ContainsListCollection |
關於此成員的描述,請參見 ContainsListCollection。 |
| IListSource.GetList() |
關於此成員的描述,請參見 GetList()。 |
| ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
填充序列化資訊物件所需的資料。DataTable |
| IXmlSerializable.GetSchema() |
關於此成員的描述,請參見 GetSchema()。 |
| IXmlSerializable.ReadXml(XmlReader) |
關於此成員的描述,請參見 ReadXml(XmlReader)。 |
| IXmlSerializable.WriteXml(XmlWriter) |
關於此成員的描述,請參見 WriteXml(XmlWriter)。 |
擴充方法
| 名稱 | Description |
|---|---|
| AsDataView(DataTable) |
建立並回傳一個啟用 LINQ 的 DataView 物件。 |
| AsEnumerable(DataTable) |
回傳一個 IEnumerable<T> 物件,其中一般參數 |
適用於
執行緒安全性
此類型適合多執行緒讀取操作。 您必須同步處理任何寫入作業。