DataTableCollection.IndexOf 方法

定義

取得指定 DataTable 物件集合中的索引。Gets the index in the collection of the specified DataTable object.

多載

IndexOf(String, String)

取得指定 DataTable 物件集合中的索引。Gets the index in the collection of the specified DataTable object.

IndexOf(DataTable)

取得指定 DataTable 物件的索引。Gets the index of the specified DataTable object.

IndexOf(String)

取得在具有指定名稱之 DataTable 物件的集合中的索引。Gets the index in the collection of the DataTable object with the specified name.

IndexOf(String, String)

取得指定 DataTable 物件集合中的索引。Gets the index in the collection of the specified DataTable object.

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

參數

tableName
String

要搜尋之 DataTable 物件的名稱。The name of the DataTable object to look for.

tableNamespace
String

要查詢之 DataTable 命名空間的名稱。The name of the DataTable namespace to look in.

傳回

Int32

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

範例

下列範例會傳回中的命名資料表索引 DataTableCollectionThe following example returns the index of a named table in the DataTableCollection.

private void GetIndexes()
{
    // Get the DataSet of a DataGrid.
    DataSet thisDataSet = (DataSet)DataGrid1.DataSource;

    // Get the DataTableCollection through the Tables property.
    DataTableCollection tables = thisDataSet.Tables;

    // Get the index of the table named "Authors", if it exists.
    if (tables.Contains("Authors"))
        System.Diagnostics.Debug.WriteLine(tables.IndexOf("Authors"));
}
Private Sub GetIndexes()
   ' Get the DataSet of a DataGrid.
   Dim thisDataSet As DataSet = CType(DataGrid1.DataSource, DataSet)

   ' Get the DataTableCollection through the Tables property.
   Dim tables As DataTableCollection = thisDataSet.Tables

   ' Get the index of the table named "Authors", if it exists.
   If tables.Contains("Authors") Then
      System.Diagnostics.Debug.WriteLine(tables.IndexOf("Authors"))
   End If
End Sub

備註

您可以 DataTable 使用屬性來指定物件的名稱 TableNameYou specify the name of the DataTable object by using the TableName property.

另請參閱

適用於

IndexOf(DataTable)

取得指定 DataTable 物件的索引。Gets the index of the specified DataTable object.

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

參數

table
DataTable

要搜尋的 DataTableThe DataTable to search for.

傳回

Int32

資料表的以零起始之索引;如果在集合中找不到此資料表,則為 -1。The zero-based index of the table, or -1 if the table is not found in the collection.

範例

下列範例會傳回中每個資料表的索引 DataTableCollectionThe following example returns the index of each table in the DataTableCollection.

private void GetIndexes()
{
    // Get the DataSet of a DataGrid.
    DataSet thisDataSet = (DataSet)DataGrid1.DataSource;

    // Get the DataTableCollection through the Tables property.
    DataTableCollection tables = thisDataSet.Tables;

    // Get the index of each table in the collection.
    foreach (DataTable table in tables)
        System.Diagnostics.Debug.WriteLine(tables.IndexOf(table));
}
Private Sub GetIndexes()
    ' Get the DataSet of a DataGrid.
    Dim thisDataSet As DataSet = CType(DataGrid1.DataSource, DataSet)

    ' Get the DataTableCollection through the Tables property.
    Dim tables As DataTableCollection = thisDataSet.Tables
    Dim table As DataTable

    ' Get the index of each table in the collection.
    For Each table In tables
       System.Diagnostics.Debug.WriteLine(tables.IndexOf(table))
    Next
End Sub

備註

IndexOf 可以使用方法來判斷給定資料表的確切索引。Use the IndexOf method to determine the exact index of a given table.

在呼叫之前 IndexOf ,您可以使用方法,測試索引或名稱) 指定的資料表 (是否存在 ContainsBefore calling IndexOf, you can test for the existence of a table (specified by either index or name) by using the Contains method.

另請參閱

適用於

IndexOf(String)

取得在具有指定名稱之 DataTable 物件的集合中的索引。Gets the index in the collection of the DataTable object with the specified name.

public:
 int IndexOf(System::String ^ tableName);
public:
 virtual int IndexOf(System::String ^ tableName);
public int IndexOf (string tableName);
public virtual int IndexOf (string tableName);
member this.IndexOf : string -> int
abstract member IndexOf : string -> int
override this.IndexOf : string -> int
Public Function IndexOf (tableName As String) As Integer
Public Overridable Function IndexOf (tableName As String) As Integer

參數

tableName
String

要搜尋之 DataTable 物件的名稱。The name of the DataTable object to look for.

傳回

Int32

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


當有兩個以上的資料表具有相同名稱但是命名空間不同時,則傳回 -1。Returns -1 when two or more tables have the same name but different namespaces. 如果資料表名稱無法完全與單一資料表相符 (即有任何名稱語意模糊),呼叫就會失敗。The call does not succeed if there is any ambiguity when matching a table name to exactly one table.

範例

下列範例會傳回中的命名資料表索引 DataTableCollectionThe following example returns the index of a named table in the DataTableCollection.

private void GetIndexes()
{
    // Get the DataSet of a DataGrid.
    DataSet thisDataSet = (DataSet)DataGrid1.DataSource;

    // Get the DataTableCollection through the Tables property.
    DataTableCollection tables = thisDataSet.Tables;

    // Get the index of the table named "Authors", if it exists.
    if (tables.Contains("Authors"))
        System.Diagnostics.Debug.WriteLine(tables.IndexOf("Authors"));
}
Private Sub GetIndexes()
   ' Get the DataSet of a DataGrid.
   Dim thisDataSet As DataSet = CType(DataGrid1.DataSource, DataSet)

   ' Get the DataTableCollection through the Tables property.
   Dim tables As DataTableCollection = thisDataSet.Tables

   ' Get the index of the table named "Authors", if it exists.
   If tables.Contains("Authors") Then
      System.Diagnostics.Debug.WriteLine(tables.IndexOf("Authors"))
   End If
End Sub

備註

您可以 DataTable 使用屬性來指定物件的名稱 TableNameYou specify the name of the DataTable object by using the TableName property.

另請參閱

適用於