DataColumnCollection.Remove 方法

定義

從集合中移除 DataColumn 物件。Removes a DataColumn object from the collection.

多載

Remove(DataColumn)

從集合中移除指定的 DataColumn 物件。Removes the specified DataColumn object from the collection.

Remove(String)

從集合移除具有指定名稱的 DataColumn 物件。Removes the DataColumn object that has the specified name from the collection.

Remove(DataColumn)

從集合中移除指定的 DataColumn 物件。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)

參數

column
DataColumn

要移除的 DataColumnThe DataColumn to remove.

例外狀況

column 參數為 nullThe 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.

範例

下列範例會使用Contains方法來判斷是否有已命名的資料行存在。The following example uses the Contains method to determine whether a named column exists. 若是如此, Item[String]屬性會傳回資料行。If so, the Item[String] property returns the column. 然後方法會檢查是否可以移除資料行; 如果是Remove ,則方法會將它移除。 CanRemoveThe 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

備註

如果藉由加入或移除資料行成功變更集合,則CollectionChanged會發生此事件。If the collection is successfully changed by adding or removing columns, the CollectionChanged event occurs.

另請參閱

Remove(String)

從集合移除具有指定名稱的 DataColumn 物件。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)

參數

name
String

要移除的資料行名稱。The name of the column to remove.

例外狀況

集合不具有含有指定名稱的資料行。The collection does not have a column with the specified name.

範例

下列範例會先使用Contains方法來判斷是否在集合中找到特定資料行。The following example first uses the Contains method to determine whether a particular column is found in the collection. 如果找到, CanRemove方法會測試是否可以移除資料行。If it is found, the CanRemove method tests whether the column can be removed. 若是如此,則會使用Remove方法移除該資料行。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

備註

如果藉由加入或移除資料行成功變更集合,則CollectionChanged會發生此事件。If the collection is successfully changed by adding or removing columns, the CollectionChanged event occurs.

另請參閱

適用於