This documentation is archived and is not being maintained.

DataColumnCollection.Contains Method

Checks whether the collection contains a column with the specified name.

[Visual Basic]
Public Function Contains( _
   ByVal name As String _
) As Boolean
public bool Contains(
 string name
public: bool Contains(
 String* name
public function Contains(
   name : String
) : Boolean;


The ColumnName of the column to check for.

Return Value

true if a column exists with this name; otherwise, false.


Use the Contains method to confirm the existence of a column before performing further operations on the column.


[Visual Basic, C#, C++] The following example uses the Contains method and the CanRemove method to determine whether the column can be removed. If so, the column is removed.

[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 | CanRemove | IndexOf