DataTableCollection.Remove 方法

定義

從集合中移除指定的 DataTable 物件。

多載

名稱 Description
Remove(String, String)

從集合中移除 DataTable 指定名稱的物件。

Remove(DataTable)

從集合中移除指定的 DataTable 物件。

Remove(String)

從集合中移除 DataTable 指定名稱的物件。

Remove(String, String)

從集合中移除 DataTable 指定名稱的物件。

public:
 void Remove(System::String ^ name, System::String ^ tableNamespace);
public void Remove(string name, string tableNamespace);
member this.Remove : string * string -> unit
Public Sub Remove (name As String, tableNamespace As String)

參數

name
String

要移除的物件名稱 DataTable

tableNamespace
String

要查的命名空間名稱 DataTable

例外狀況

該集合沒有指定名稱的表格。

範例

以下範例使用 和 CanRemove 方法Contains來測試命名資料表是否存在且是否可被移除。 若是,則會呼叫該 Remove 方法以移除該表格。

private void RemoveTables()
{
    // Set the name of the table to test for and remove.
    string name = "Suppliers";

    // Presuming a DataGrid is displaying more than one table, get its DataSet.
    DataSet thisDataSet = (DataSet)DataGrid1.DataSource;
    DataTableCollection tablesCol = thisDataSet.Tables;
    if (tablesCol.Contains(name) && tablesCol.CanRemove(tablesCol[name]))
        tablesCol.Remove(name);
}
Private Sub RemoveTables()
   ' Set the name of the table to test for and remove.
   Dim name As String = "Suppliers"

   ' Presuming a DataGrid is displaying more than one table, get its DataSet.
   Dim thisDataSet As DataSet = CType(DataGrid1.DataSource, DataSet)
   Dim tablesCol As DataTableCollection = thisDataSet.Tables
   If tablesCol.Contains(name) _
   And tablesCol.CanRemove(tablesCol(name)) Then 
      tablesCol.Remove(name)
   End If
End Sub

備註

當某個表格成功移除時,該 CollectionChanged 事件就會發生。

要判斷某個表格是否存在且在呼叫 Remove前可移除,請使用 和 Contains 方法 CanRemove

另請參閱

適用於

Remove(DataTable)

從集合中移除指定的 DataTable 物件。

public:
 void Remove(System::Data::DataTable ^ table);
public void Remove(System.Data.DataTable table);
member this.Remove : System.Data.DataTable -> unit
Public Sub Remove (table As DataTable)

參數

table
DataTable

DataTable要移除。

例外狀況

表格指定的值為 null

這張桌子不屬於這個收藏。

-或-

桌子是關係的一部分。

範例

以下範例使用該 CanRemove 方法來測試每個表格是否能從 DataSet中移除。 若是,則會呼叫該 Remove 方法以移除該表格。

public static void DataTableCollectionCanRemove()
{
    // create a DataSet with two tables
    DataSet dataSet = new DataSet();

    // create Customer table
    DataTable customersTable = new DataTable("Customers");
    customersTable.Columns.Add("customerId",
        typeof(int) ).AutoIncrement = true;
    customersTable.Columns.Add("name",
        typeof(string));
    customersTable.PrimaryKey = new DataColumn[]
        { customersTable.Columns["customerId"] };

    // create Orders table
    DataTable ordersTable = new DataTable("Orders");
    ordersTable.Columns.Add("orderId",
        typeof(int) ).AutoIncrement = true;
    ordersTable.Columns.Add("customerId",
        typeof(int) );
    ordersTable.Columns.Add("amount",
        typeof(double));
    ordersTable.PrimaryKey = new DataColumn[]
        { ordersTable.Columns["orderId"] };

    dataSet.Tables.AddRange(new DataTable[]
        {customersTable, ordersTable });

    // remove all tables
    // check if table can be removed and then
    // remove it, cannot use a foreach when
    // removing items from a collection
    while(dataSet.Tables.Count > 0)
    {
        DataTable table = dataSet.Tables[0];
        if(dataSet.Tables.CanRemove(table))
        {
            dataSet.Tables.Remove(table);
        }
    }

    Console.WriteLine("dataSet has {0} tables",
        dataSet.Tables.Count);
}
Public Sub Main
DataTableCollectionCanRemove
End Sub

Public Sub DataTableCollectionCanRemove()
    ' create a DataSet with two tables
    Dim dataSet As New DataSet()

    ' create Customer table
    Dim customersTable As New DataTable("Customers")
    customersTable.Columns.Add("customerId", _
        System.Type.GetType("System.Integer")).AutoIncrement = True
    customersTable.Columns.Add("name", _
        System.Type.GetType("System.String"))
    customersTable.PrimaryKey = New DataColumn() _
        {customersTable.Columns("customerId")}

    ' create Orders table
    Dim ordersTable As New DataTable("Orders")
    ordersTable.Columns.Add("orderId", _
        System.Type.GetType("System.Integer")).AutoIncrement = True
    ordersTable.Columns.Add("customerId", _
        System.Type.GetType("System.Integer"))
    ordersTable.Columns.Add("amount", _
        System.Type.GetType("System.Double"))
    ordersTable.PrimaryKey = New DataColumn() _
        {ordersTable.Columns("orderId")}

    dataSet.Tables.AddRange(New DataTable() {customersTable, ordersTable})

    ' remove all tables
    ' check if table can be removed and then
    ' remove it, cannot use a foreach when
    ' removing items from a collection
    Do While (dataSet.Tables.Count > 0)
        Dim table As DataTable = dataSet.Tables(0)
        If (dataSet.Tables.CanRemove(table)) Then
            dataSet.Tables.Remove(table)
        End If
    Loop

Console.WriteLine("dataSet has {0} tables", dataSet.Tables.Count)
End Sub

備註

當某個表格成功移除時,該 CollectionChanged 事件就會發生。

要判斷某個表格是否存在且在呼叫 Remove前可移除,請使用 和 Contains 方法 CanRemove

另請參閱

適用於

Remove(String)

從集合中移除 DataTable 指定名稱的物件。

public:
 void Remove(System::String ^ name);
public void Remove(string name);
member this.Remove : string -> unit
Public Sub Remove (name As String)

參數

name
String

要移除的物件名稱 DataTable

例外狀況

該集合沒有指定名稱的表格。

範例

以下範例使用 和 CanRemove 方法Contains來測試命名資料表是否存在且是否可被移除。 若是,則會呼叫該 Remove 方法以移除該表格。

private void RemoveTables()
{
    // Set the name of the table to test for and remove.
    string name = "Suppliers";

    // Presuming a DataGrid is displaying more than one table, get its DataSet.
    DataSet thisDataSet = (DataSet)DataGrid1.DataSource;
    DataTableCollection tablesCol = thisDataSet.Tables;
    if (tablesCol.Contains(name) && tablesCol.CanRemove(tablesCol[name]))
        tablesCol.Remove(name);
}
Private Sub RemoveTables()
   ' Set the name of the table to test for and remove.
   Dim name As String = "Suppliers"

   ' Presuming a DataGrid is displaying more than one table, get its DataSet.
   Dim thisDataSet As DataSet = CType(DataGrid1.DataSource, DataSet)
   Dim tablesCol As DataTableCollection = thisDataSet.Tables
   If tablesCol.Contains(name) _
   And tablesCol.CanRemove(tablesCol(name)) Then 
      tablesCol.Remove(name)
   End If
End Sub

備註

當某個表格成功移除時,該 CollectionChanged 事件就會發生。

要判斷某個表格是否存在且在呼叫 Remove前可移除,請使用 和 Contains 方法 CanRemove

另請參閱

適用於