DataTable 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示記憶體中資料的一個資料表。
public ref class DataTable : System::ComponentModel::MarshalByValueComponent, System::ComponentModel::IListSource, System::ComponentModel::ISupportInitialize, System::ComponentModel::ISupportInitializeNotification, System::Runtime::Serialization::ISerializable, System::Xml::Serialization::IXmlSerializable
public ref class DataTable
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
public class DataTable : System.ComponentModel.MarshalByValueComponent, System.ComponentModel.IListSource, System.ComponentModel.ISupportInitialize, System.ComponentModel.ISupportInitializeNotification, System.Runtime.Serialization.ISerializable, System.Xml.Serialization.IXmlSerializable
public class DataTable
[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
type DataTable = class
inherit MarshalByValueComponent
interface IListSource
interface ISupportInitialize
interface ISupportInitializeNotification
interface ISerializable
interface IXmlSerializable
type DataTable = class
[<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
Public Class DataTable
Inherits MarshalByValueComponent
Implements IListSource, ISerializable, ISupportInitialize, ISupportInitializeNotification, IXmlSerializable
Public Class DataTable
Public Class DataTable
Inherits MarshalByValueComponent
Implements IListSource, ISerializable, ISupportInitialize
Public Class DataTable
Inherits MarshalByValueComponent
Implements IListSource, ISerializable, ISupportInitializeNotification, IXmlSerializable
- 繼承
- 繼承
-
DataTable
- 衍生
- 屬性
- 實作
範例
下列範例會建立兩 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 備註。
建構函式
DataTable() |
不使用引數來初始化 DataTable 類別的新執行個體。 |
DataTable(SerializationInfo, StreamingContext) |
已淘汰.
使用序列化資料,初始化 DataTable 類別的新執行個體。 |
DataTable(String) |
使用指定的資料表名稱,初始化 DataTable 類別的新執行個體。 |
DataTable(String, String) |
使用指定的資料表名稱和命名空間,初始化 DataTable 類別的新執行個體。 |
欄位
fInitInProgress |
檢查初始化是否仍在進行中。 初始化發生於執行階段。 |
屬性
CaseSensitive |
指示字串比較在資料表中是否區分大小寫。 |
ChildRelations |
為這個 DataTable 取得子關聯的集合。 |
Columns |
取得屬於這個資料表的資料行集合。 |
Constraints |
取得這個資料表所維護的條件約束 (Constraint) 集合。 |
Container |
取得元件的容器。 (繼承來源 MarshalByValueComponent) |
DataSet |
取得這個資料表所屬的 DataSet。 |
DefaultView |
取得可能含有已篩選檢視表或游標位置的資料表自訂檢視表。 |
DesignMode |
取得值,表示元件目前是否處於設計模式。 (繼承來源 MarshalByValueComponent) |
DisplayExpression |
取得或設定會傳回用來在使用者介面中表示這個資料表之值的運算式。
|
Events |
取得附加在這個元件上的事件處理常式清單。 (繼承來源 MarshalByValueComponent) |
ExtendedProperties |
取得自訂使用者資訊的集合。 |
HasErrors |
取得值,指出資料表所屬 DataSet 的任何資料表中的任何資料列是否存在錯誤。 |
IsInitialized |
取得值,指出 DataTable 是否已初始化。 |
Locale |
取得或設定用來在資料表中比較字串的地區設定 (Locale) 資訊。 |
MinimumCapacity |
取得或設定這個資料表的初始開始大小。 |
Namespace |
為在 DataTable 中所儲存資料的 XML 表示取得或設定命名空間。 |
ParentRelations |
為這個 DataTable 取得父關聯的集合。 |
Prefix |
為在 DataTable 中所儲存資料的 XML 表示取得或設定命名空間。 |
PrimaryKey |
取得或設定資料行的陣列,這些資料行是做為資料表 (Data Table) 之主索引鍵。 |
RemotingFormat |
取得或設定序列化格式。 |
Rows |
取得屬於這個資料表的資料列集合。 |
Site | |
TableName |
取得或設定 DataTable 的名稱。 |
方法
事件
ColumnChanged |
發生在值已經為 DataColumn 中指定的 DataRow 變更之後。 |
ColumnChanging |
發生在值正在為 DataColumn 中指定的 DataRow 變更之後。 |
Disposed |
加入事件處理常式來接聽元件上的 Disposed 事件。 (繼承來源 MarshalByValueComponent) |
Initialized |
發生於 DataTable 初始化之後。 |
RowChanged |
成功變更 DataRow 後發生。 |
RowChanging |
發生在 DataRow 正在變更時。 |
RowDeleted |
發生在資料表中的資料列已經刪除之後。 |
RowDeleting |
發生在資料表中的資料列將要刪除之前。 |
TableCleared |
發生在清除 DataTable 之後。 |
TableClearing |
發生於清除 DataTable 時。 |
TableNewRow |
發生在插入新的 DataRow 時。 |
明確介面實作
IListSource.ContainsListCollection |
如需這個成員的說明,請參閱 ContainsListCollection。 |
IListSource.GetList() |
如需這個成員的說明,請參閱 GetList()。 |
ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
以序列化 DataTable 所需的資料,填入序列化資訊物件。 |
IXmlSerializable.GetSchema() |
如需這個成員的說明,請參閱 GetSchema()。 |
IXmlSerializable.ReadXml(XmlReader) |
如需這個成員的說明,請參閱 ReadXml(XmlReader)。 |
IXmlSerializable.WriteXml(XmlWriter) |
如需這個成員的說明,請參閱 WriteXml(XmlWriter)。 |
擴充方法
適用於
執行緒安全性
此類型適用於多線程讀取作業。 您必須同步處理任何寫入作業。