Olvasás angol nyelven Szerkesztés

Megosztás a következőn keresztül:


DataTableCollection.IndexOf Method

Definition

Gets the index in the collection of the specified DataTable object.

Overloads

IndexOf(String, String)

Gets the index in the collection of the specified DataTable object.

IndexOf(DataTable)

Gets the index of the specified DataTable object.

IndexOf(String)

Gets the index in the collection of the DataTable object with the specified name.

IndexOf(String, String)

Source:
DataTableCollection.cs
Source:
DataTableCollection.cs
Source:
DataTableCollection.cs

Gets the index in the collection of the specified DataTable object.

C#
public int IndexOf(string tableName, string tableNamespace);

Parameters

tableName
String

The name of the DataTable object to look for.

tableNamespace
String

The name of the DataTable namespace to look in.

Returns

The zero-based index of the DataTable with the specified name, or -1 if the table does not exist in the collection.

Examples

The following example returns the index of a named table in the DataTableCollection.

C#
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"));
}

Remarks

You specify the name of the DataTable object by using the TableName property.

See also

Applies to

.NET 10 és más verziók
Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

IndexOf(DataTable)

Source:
DataTableCollection.cs
Source:
DataTableCollection.cs
Source:
DataTableCollection.cs

Gets the index of the specified DataTable object.

C#
public int IndexOf(System.Data.DataTable? table);
C#
public int IndexOf(System.Data.DataTable table);
C#
public virtual int IndexOf(System.Data.DataTable table);

Parameters

table
DataTable

The DataTable to search for.

Returns

The zero-based index of the table, or -1 if the table is not found in the collection.

Examples

The following example returns the index of each table in the DataTableCollection.

C#
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));
}

Remarks

Use the IndexOf method to determine the exact index of a given table.

Before calling IndexOf, you can test for the existence of a table (specified by either index or name) by using the Contains method.

See also

Applies to

.NET 10 és más verziók
Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

IndexOf(String)

Source:
DataTableCollection.cs
Source:
DataTableCollection.cs
Source:
DataTableCollection.cs

Gets the index in the collection of the DataTable object with the specified name.

C#
public int IndexOf(string? tableName);
C#
public int IndexOf(string tableName);
C#
public virtual int IndexOf(string tableName);

Parameters

tableName
String

The name of the DataTable object to look for.

Returns

The zero-based index of the DataTable with the specified name, or -1 if the table does not exist in the collection.

Examples

The following example returns the index of a named table in the DataTableCollection.

C#
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"));
}

Remarks

You specify the name of the DataTable object by using the TableName property.

This method 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.

See also

Applies to

.NET 10 és más verziók
Termék Verziók
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1