Export (0) Print
Expand All

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
[C#]
public bool CanRemove(
 DataColumn column
);
[C++]
public: bool CanRemove(
 DataColumn* column
);
[JScript]
public function CanRemove(
   column : DataColumn
) : Boolean;

Parameters

column
A DataColumn in the collection.

Return Value

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

Exceptions

Exception Type Condition
ArgumentNullException The column parameter is a null reference (Nothing in Visual Basic).
ArgumentException 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.

Remarks

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.

Example

[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

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

[C++] 
private:
void RemoveColumn(String* colName, DataTable* table)
{
    DataColumnCollection* cols = table->Columns;
 
    if (cols->Contains(colName))
       if (cols->CanRemove(cols->Item[colName]))
           cols->Remove(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.

Requirements

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

Show:
© 2014 Microsoft