System.Data Namespace DataColumnCollection Class
Checks whether a given column can be removed from the collection.
[ VB ]
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;
- column
- A DataColumn in the collection.
true if the column can be removed; otherwise, false.
Exception Type |
Condition |
ArgumentNullException |
The column parameter is a null reference. |
ArgumentException |
The column does not belong to this collection, or the column is part of a relationship, or another column's compute expression depends on this column. |
The CanRemove method performs several checks before returning a true or false including the following: whether the column exists, belongs to the table, 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 reference it.
The following example first uses the Contains method to determine if a particular column exists 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.
private void RemoveColumn ( DataSet ds ) {
// get the DataColumnCollection from a DataTable in a DataSet.
DataColumnCollection cols = ds.Tables [ "Orders" ].Columns;
if ( cols.Contains ( "Totals" ) ) {
if ( cols.CanRemove ( cols ( "Totals" ) ) ) {
cols.Remove ( "Totals" );
}
}
}
Private Sub RemoveColumn ( ByVal ds As DataSet )
' get the DataColumnCollection from a DataTable in a DataSet.
Dim cols As DataColumnCollection = ds.Tables ( "Orders" ).Columns
If cols.Contains ( "Totals" ) Then
If cols.CanRemove ( cols ( "Totals" ) ) Then
cols.Remove ( "Totals" )
End If
End If
End Sub |
|
C# |
VB |
DataColumnCollection Members Contains Remove