Edit

Share via


DataColumnCollection.Contains(String) Method

Definition

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

public:
 bool Contains(System::String ^ name);
public bool Contains (string name);
member this.Contains : string -> bool
Public Function Contains (name As String) As Boolean

Parameters

name
String

The ColumnName of the column to look for.

Returns

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

Examples

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.

private void RemoveColumn(string columnName, DataTable table)
{
    DataColumnCollection columns = table.Columns;

    if (columns.Contains(columnName))
        if (columns.CanRemove(columns[columnName]))
            columns.Remove(columnName);
}
Private Sub RemoveColumn(columnName As String, table As DataTable)
    Dim columns As DataColumnCollection = table.Columns

    If columns.Contains(columnName) Then 
        If columns.CanRemove(columns(columnName)) Then 
            columns.Remove(columnName)
        End If
    End If
End Sub

Remarks

Use the Contains method to confirm the existence of a column before you perform additional operations on the column. The method returns false when two or more columns have the same name but different namespaces. The call does not succeed if there is any ambiguity when matching a column name to exactly one column.

Applies to

See also