This documentation is archived and is not being maintained.

DataColumnCollection.CanRemove Method

Checks whether a given column can be removed from the collection.

[Visual Basic]
Public Function CanRemove( _
   ByVal column As DataColumn _
) As Boolean
public bool CanRemove(
 DataColumn column
public: bool CanRemove(
 DataColumn* column
public function CanRemove(
   column : DataColumn
) : Boolean;


A DataColumn in the collection.

Return Value

true if the column can be removed; otherwise, false.


Exception Type Condition
ArgumentNullException The column parameter is a null reference (Nothing in Visual Basic).
ArgumentException The column does not belong to this collection.


The column is part of a relationship.


Another column's expression depends on this column.


The CanRemove method performs several checks before returning a true or false result, including the following: whether the column exists, belongs to the table, or is involved in a constraint or relation.

Use the CanRemove method before attempting to remove any column from a collection. You can also use the Contains method to determine if a particular column exists before attempting to remove it.


[Visual Basic, C#, C++] The following example first uses the Contains method to determine if a particular column is found in the collection. If found, the CanRemove method tests whether the column can be removed. If so, the column is removed with the Remove method.

[Visual Basic] 
Private Sub RemoveColumn(colName As String, table As DataTable)
    Dim cols As DataColumnCollection = table.Columns
    If cols.Contains(colName) Then 

       If cols.CanRemove(cols(colName)) Then cols.Remove(colName)

    End If
 End Sub

private void RemoveColumn(string colName, DataTable table)
    DataColumnCollection cols = table.Columns;
    if (cols.Contains(colName))
       if (cols.CanRemove(cols[colName]))

void RemoveColumn(String* colName, DataTable* table)
    DataColumnCollection* cols = table->Columns;
    if (cols->Contains(colName))
       if (cols->CanRemove(cols->Item[colName]))

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataColumnCollection Class | DataColumnCollection Members | System.Data Namespace | Contains | Remove