Редагувати

Поділитися через


DataColumnCollection.Remove Method

Definition

Removes a DataColumn object from the collection.

Overloads

Remove(DataColumn)

Removes the specified DataColumn object from the collection.

Remove(String)

Removes the DataColumn object that has the specified name from the collection.

Remove(DataColumn)

Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs

Removes the specified DataColumn object from the collection.

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

Parameters

column
DataColumn

The DataColumn to remove.

Exceptions

The column parameter is null.

The column does not belong to this collection.

-Or-

The column is part of a relationship.

-Or-

Another column's expression depends on this column.

Examples

The following example uses the Contains method to determine whether a named column exists. If so, the Item[] property returns the column. The CanRemove method then checks whether the column can be removed; if so, the Remove method removes it.

private void TestAndRemove(DataColumn colToRemove)
{
    DataColumnCollection columns;
    // Get the DataColumnCollection from a DataTable in a DataSet.
    columns = DataSet1.Tables["Orders"].Columns;

    if(columns.Contains(colToRemove.ColumnName))
    {
        columns.Remove(colToRemove);
    }
}
Private Sub TestAndRemove(ByVal colToRemove As DataColumn)
    ' Get the DataColumnCollection from a DataTable in a DataSet.
    Dim columns As DataColumnCollection = _
        DataSet1.Tables("Orders").Columns
 
    If columns.Contains(colToRemove.ColumnName) Then
       columns.Remove(colToRemove)
    End If
End Sub

Remarks

If the collection is successfully changed by adding or removing columns, the CollectionChanged event occurs.

See also

Applies to

Remove(String)

Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs
Source:
DataColumnCollection.cs

Removes the DataColumn object that has the specified name from the collection.

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

Parameters

name
String

The name of the column to remove.

Exceptions

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

Examples

The following example first uses the Contains method to determine whether a particular column is found in the collection. If it is found, the CanRemove method tests whether the column can be removed. If so, the column is removed with the Remove method.

private void RemoveColumnByName(string columnName)
{
    // Get the DataColumnCollection from a DataTable in a DataSet.
    DataColumnCollection columns =
        ds.Tables["Suppliers"].Columns;

    if(columns.Contains(columnName))
        if(columns.CanRemove(columns[columnName]))
            columns.Remove(columnName);
}
Private Sub RemoveColumnByName(columnName As String)

    ' Get the DataColumnCollection from a DataTable in a DataSet.
    Dim columns As DataColumnCollection = _
        DataSet1.Tables("Orders").Columns

    If columns.Contains(columnName) Then 
        If columns.CanRemove(columns(columnName)) Then 
            columns.Remove(columnName)
        End If
    End If
 End Sub

Remarks

If the collection is successfully changed by adding or removing columns, the CollectionChanged event occurs.

See also

Applies to