Returns the index of a column specified by name.
[Visual Basic] Public Function IndexOf( _ ByVal columnName As String _ ) As Integer [C#] public int IndexOf( string columnName ); [C++] public: int IndexOf( String* columnName ); [JScript] public function IndexOf( columnName : String ) : int;
The index of the column specified by columnName; otherwise,-1 if the column cannot be found.
The IndexOf method is not case-sensitive.
The following example uses the Contains method to determine if a specified column exists in a columns collection. If so, the IndexOf method returns the index of the column.
[Visual Basic]
Dim iCol As Integer Dim cols As ColumnsCollection ' Get the ColumnsCollection from a DataTable in a DataSet. cols = DataSet1.Tables("Orders").Columns If cols.Contains("id") Then iCol = cols.IndexOf("id") Console.WriteLine(iCol, cols(iCol).DataType.ToString) End If
ColumnsCollection Class | ColumnsCollection Members | System.Data Namespace