DataColumnCollection.CanRemove(DataColumn) 方法

定義

檢查是否可以從集合移除特定的資料行。Checks whether a specific column can be removed from the collection.

public:
 bool CanRemove(System::Data::DataColumn ^ column);
public bool CanRemove (System.Data.DataColumn column);
member this.CanRemove : System.Data.DataColumn -> bool
Public Function CanRemove (column As DataColumn) As Boolean

參數

column
DataColumn

在集合中的 DataColumnA DataColumn in the collection.

傳回

如果可以移除該資料行則為 truetrue if the column can be removed. 否則為 falsefalse if, - column 參數是 null- The column parameter is null.

- 資料行不屬於此集合。- The column does not belong to this collection.

- 資料行是關聯性的一部分。- The column is part of a relationship.

- 另一個資料行的運算式相依於此資料行。- Another column's expression depends on this column.

例外狀況

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

範例

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

private void RemoveColumn(string columnName, DataTable table)
{
    DataColumnCollection columns = table.Columns;
    if (columns.Contains(columnName))
        if (columns.CanRemove(columns[columnName]))
            columns.Remove(columnName);
}
Private Sub RemoveColumn( _
    columnName As String, table As DataTable)

    Dim columns As DataColumnCollection = table.Columns
    If columns.Contains(columnName) Then 
        If columns.CanRemove(columns(columnName)) Then 
           columns.Remove(columnName)
        End If
    End If
End Sub

備註

CanRemove 方法會在傳回 truefalse 結果之前,先執行數個檢查。The CanRemove method performs several checks before returning a true or false result. 這包括下列各項:資料行是否存在、屬於資料表,或包含在條件約束或關聯中。This includes the following: whether the column exists, belongs to the table, or is involved in a constraint or relation.

嘗試從集合中移除任何資料行之前,請先使用 CanRemove 方法。Use the CanRemove method before you try to remove any column from a collection. 您也可以使用 Contains 方法來判斷特定資料行是否存在,然後再嘗試將它移除。You can also use the Contains method to determine whether a particular column exists before you try to remove it.

適用於

另請參閱