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.

返回

具有指定名称的 DataTable 的从零开始的索引;或者如果集合中不存在该表,则为 –1。The zero-based index of the DataTable with the specified name, or -1 if the table does not exist in the collection.

示例

下面的示例返回中DataTableCollection指定表的索引。The 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:
 virtual int IndexOf(System::Data::DataTable ^ table);
public virtual int IndexOf (System.Data.DataTable table);
abstract member IndexOf : System.Data.DataTable -> int
override this.IndexOf : System.Data.DataTable -> int
Public Overridable Function IndexOf (table As DataTable) As Integer

参数

table
DataTable

要搜索的 DataTableThe DataTable to search for.

返回

表从零开始的索引,如果在集合中未找到该表,则为 -1。The zero-based index of the table, or -1 if the table is not found in the collection.

示例

下面的示例返回中DataTableCollection每个表的索引。The 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之前,可以Contains使用方法测试表是否存在(由索引或名称指定)。Before 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:
 virtual int IndexOf(System::String ^ tableName);
public virtual int IndexOf (string tableName);
abstract member IndexOf : string -> int
override this.IndexOf : string -> int
Public Overridable Function IndexOf (tableName As String) As Integer

参数

tableName
String

要查找的 DataTable 对象的名称。The name of the DataTable object to look for.

返回

具有指定名称的 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.

示例

下面的示例返回中DataTableCollection指定表的索引。The 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.

另请参阅

适用于