Olvasás angol nyelven Szerkesztés

Megosztás a következőn keresztül:


DataTableCollection.Remove Method

Definition

Removes a specified DataTable object from the collection.

Overloads

Remove(String, String)

Removes the DataTable object with the specified name from the collection.

Remove(DataTable)

Removes the specified DataTable object from the collection.

Remove(String)

Removes the DataTable object with the specified name from the collection.

Remove(String, String)

Source:
DataTableCollection.cs
Source:
DataTableCollection.cs
Source:
DataTableCollection.cs

Removes the DataTable object with the specified name from the collection.

public void Remove (string name, string tableNamespace);

Parameters

name
String

The name of the DataTable object to remove.

tableNamespace
String

The name of the DataTable namespace to look in.

Exceptions

The collection does not have a table with the specified name.

Examples

The following example uses the Contains and CanRemove methods to test whether a named table exists and can be removed. If so, the Remove method is called to remove the table.

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);
}

Remarks

The CollectionChanged event occurs when a table is successfully removed.

To determine whether a given table exists and can be removed before invoking Remove, use the Contains and the CanRemove methods.

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Remove(DataTable)

Source:
DataTableCollection.cs
Source:
DataTableCollection.cs
Source:
DataTableCollection.cs

Removes the specified DataTable object from the collection.

public void Remove (System.Data.DataTable table);

Parameters

table
DataTable

The DataTable to remove.

Exceptions

The value specified for the table is null.

The table does not belong to this collection.

-or-

The table is part of a relationship.

Examples

The following example uses the CanRemove method to test whether each table can be removed from a DataSet. If so, the Remove method is called to remove the table.

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);
}

Remarks

The CollectionChanged event occurs when a table is successfully removed.

To determine whether a given table exists and can be removed before invoking Remove, use the Contains and the CanRemove methods.

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Remove(String)

Source:
DataTableCollection.cs
Source:
DataTableCollection.cs
Source:
DataTableCollection.cs

Removes the DataTable object with the specified name from the collection.

public void Remove (string name);

Parameters

name
String

The name of the DataTable object to remove.

Exceptions

The collection does not have a table with the specified name.

Examples

The following example uses the Contains and CanRemove methods to test whether a named table exists and can be removed. If so, the Remove method is called to remove the table.

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);
}

Remarks

The CollectionChanged event occurs when a table is successfully removed.

To determine whether a given table exists and can be removed before invoking Remove, use the Contains and the CanRemove methods.

See also

Applies to

.NET 9 és más verziók
Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1