DataRowCollection.Find Método

Definición

Obtiene un DataRow con el valor PrimaryKey especificado.

Sobrecargas

Find(Object[])

Obtiene la fila que contiene los valores de clave principal especificados.

Find(Object)

Obtiene la fila especificada por el valor de clave principal.

Comentarios

El rendimiento debe ser una operación de O(log n).

Find(Object[])

Obtiene la fila que contiene los valores de clave principal especificados.

public:
 System::Data::DataRow ^ Find(cli::array <System::Object ^> ^ keys);
public System.Data.DataRow? Find (object?[] keys);
public System.Data.DataRow Find (object[] keys);
member this.Find : obj[] -> System.Data.DataRow
Public Function Find (keys As Object()) As DataRow

Parámetros

keys
Object[]

Matriz de valores de clave principal que se desea buscar. El tipo de la matriz es Object.

Devoluciones

DataRow

Un objeto DataRow que contiene los valores de clave principal especificados; de lo contrario, un valor null si el valor de clave principal no existe en la colección DataRowCollection.

Excepciones

Ninguna fila corresponde a este valor de índice.

La tabla no contiene ninguna clave principal.

Ejemplos

En el ejemplo siguiente se usan los valores de una matriz para buscar una fila específica en una colección de DataRow objetos . El método supone que existe con DataTable tres columnas de clave principal. Después de crear una matriz de los valores, el código usa el Find método con la matriz para obtener el objeto concreto que desee.

private void FindInMultiPKey(DataTable table)
{
    // Create an array for the key values to find.
    object[]findTheseVals = new object[3];

    // Set the values of the keys to find.
    findTheseVals[0] = "John";
    findTheseVals[1] = "Smith";
    findTheseVals[2] = "5 Main St.";

    DataRow foundRow = table.Rows.Find(findTheseVals);
    // Display column 1 of the found row.
    if(foundRow != null)
        Console.WriteLine(foundRow[1]);
}
 Private Sub FindInMultiPKey(ByVal table As DataTable)
    ' Create an array for the key values to find.
    Dim findTheseVals(2) As Object

    ' Set the values of the keys to find.
    findTheseVals(0) = "John"
    findTheseVals(1) = "Smith"
    findTheseVals(2) = "5 Main St."

    Dim foundRow As DataRow = table.Rows.Find(findTheseVals)
    ' Display column 1 of the found row.
    If Not (foundRow Is Nothing) Then
        Console.WriteLine(foundRow(1).ToString())
    End If
End Sub

Comentarios

Para usar el Find método , el DataTable objeto al que pertenece el DataRowCollection objeto debe tener al menos una columna designada como columna de clave principal. Cuando dos o más filas tienen el mismo valor de clave principal, se devuelve la primera fila encontrada. Esto ocurre cuando EnforceConstraints se establece en false. Consulte la PrimaryKey propiedad para obtener más información sobre cómo crear una PrimaryKey columna o una matriz de objetos cuando la tabla tiene más de DataColumn una clave principal.

Consulte también

Se aplica a

Find(Object)

Obtiene la fila especificada por el valor de clave principal.

public:
 System::Data::DataRow ^ Find(System::Object ^ key);
public System.Data.DataRow? Find (object? key);
public System.Data.DataRow Find (object key);
member this.Find : obj -> System.Data.DataRow
Public Function Find (key As Object) As DataRow

Parámetros

key
Object

Valor de clave principal del DataRow que se desea buscar.

Devoluciones

DataRow

Objeto DataRow que contiene el valor de clave principal especificado; de lo contrario, un valor null si el valor de clave principal no existe en la colección DataRowCollection.

Excepciones

La tabla no contiene ninguna clave principal.

Ejemplos

En el ejemplo siguiente se usa el Find método para buscar el valor de clave principal "2" en una colección de DataRow objetos . El método devuelve el objeto específico DataRow que le permite cambiar sus valores, según sea necesario.

private void FindInPrimaryKeyColumn(DataTable table,
    long pkValue)
{
    // Find the number pkValue in the primary key
    // column of the table.
    DataRow foundRow = table.Rows.Find(pkValue);

    // Print the value of column 1 of the found row.
    if(foundRow != null)
        Console.WriteLine(foundRow[1]);
}
 Private Sub FindInPrimaryKeyColumn(ByVal table As DataTable, _
    ByVal pkValue As Long)
    ' Find the number pkValue in the primary key 
    ' column of the table.
    Dim foundRow As DataRow = table.Rows.Find(pkValue)

    ' Print the value of column 1 of the found row.
    If Not (foundRow Is Nothing) Then
        Console.WriteLine(foundRow(1).ToString())
    End If
End Sub

Comentarios

Para usar el Find método , el DataTable objeto al que pertenece el DataRowCollection objeto debe tener al menos una columna designada como columna de clave principal. Consulte la PrimaryKey propiedad para obtener más información sobre cómo crear una columna de clave principal.

Consulte también

Se aplica a