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
[C#]
public bool Contains(
 string name
);
[C++]
public: bool Contains(
 String* name
);
[JScript]
public function Contains(
   name : String
) : Boolean;

Parameters

name
The ColumnName of the column to check for.

Return Value

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

Remarks

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

Example

[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

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

Show: