DataColumnCollection.IndexOf 方法

定義

搜尋指定的 DataColumn,並傳回集合中第一個符合項目之以零起始的索引。Searches for the specified DataColumn and returns the zero-based index of the first occurrence within the collection.

多載

IndexOf(DataColumn)

取得由名稱指定的資料行索引。Gets the index of a column specified by name.

IndexOf(String)

取得具有特定名稱 (名稱不區分大小寫) 的資料行索引。Gets the index of the column with the specific name (the name is not case sensitive).

IndexOf(DataColumn)

取得由名稱指定的資料行索引。Gets the index of a column specified by name.

public:
 virtual int IndexOf(System::Data::DataColumn ^ column);
public virtual int IndexOf (System.Data.DataColumn column);
abstract member IndexOf : System.Data.DataColumn -> int
override this.IndexOf : System.Data.DataColumn -> int
Public Overridable Function IndexOf (column As DataColumn) As Integer

參數

column
DataColumn

要傳回的資料行名稱。The name of the column to return.

傳回

如果找到,則為 column 指定的資料行索引,否則為 -1。The index of the column specified by column if it is found; otherwise, -1.

範例

下列範例會使用Contains方法來判斷指定的資料行是否存在於DataColumnCollection中。The following example uses the Contains method to determine whether a specified column exists in a DataColumnCollection. 若是如此, IndexOf方法會傳回資料行的索引。If so, the IndexOf method returns the index of the column.

private void GetIndex(DataTable table)
{
    DataColumnCollection columns = table.Columns;
    if(columns.Contains("City")) 
    {
        Console.WriteLine(columns.IndexOf("City"));
    }
}
Private Sub GetIndex(ByVal table As DataTable)
    Dim iCol As Integer
    Dim columns As DataColumnCollection = table.Columns
    If columns.Contains("City") Then
       Console.WriteLine(columns.IndexOf("City"))
    End If
End Sub

備註

IndexOf方法不會區分大小寫。The IndexOf method is not case-sensitive.

IndexOf(String)

取得具有特定名稱 (名稱不區分大小寫) 的資料行索引。Gets the index of the column with the specific name (the name is not case sensitive).

public:
 int IndexOf(System::String ^ columnName);
public int IndexOf (string columnName);
member this.IndexOf : string -> int
Public Function IndexOf (columnName As String) As Integer

參數

columnName
String

要尋找的資料行名稱。The name of the column to find.

傳回

具有指定名稱的資料行之以零起始的索引,如果集合中沒有該資料行,則為 -1。The zero-based index of the column with the specified name, or -1 if the column does not exist in the collection.

適用於