BindingSource.Find Metodo

Definizione

Trovare l'elemento specificato nell'origine dati.

Overload

Find(PropertyDescriptor, Object)

Cerca l'indice dell'elemento con il descrittore di proprietà specificato.

Find(String, Object)

Restituisce l'indice dell'elemento dell'elenco con il nome e il valore di proprietà specificati.

Find(PropertyDescriptor, Object)

Cerca l'indice dell'elemento con il descrittore di proprietà specificato.

public:
 virtual int Find(System::ComponentModel::PropertyDescriptor ^ prop, System::Object ^ key);
public virtual int Find (System.ComponentModel.PropertyDescriptor prop, object key);
abstract member Find : System.ComponentModel.PropertyDescriptor * obj -> int
override this.Find : System.ComponentModel.PropertyDescriptor * obj -> int
Public Overridable Function Find (prop As PropertyDescriptor, key As Object) As Integer

Parametri

prop
PropertyDescriptor

Oggetto PropertyDescriptor da cercare.

key
Object

Valore di prop da cercare.

Restituisce

Indice in base zero dell'elemento con il valore specificato per PropertyDescriptor.

Implementazioni

Eccezioni

L'elenco sottostante non è di tipo IBindingList .

Esempio

Nell'esempio di codice riportato di seguito viene illustrato come utilizzare il metodo Find. Per l'esempio completo, vedere l'argomento panoramica della classe.

private void button1_Click(object sender, EventArgs e)
{
    if (binding1.SupportsSearching != true)
    {
        MessageBox.Show("Cannot search the list.");
    }
    else
    {
        int foundIndex = binding1.Find("Name", textBox1.Text);
        if (foundIndex > -1)
            listBox1.SelectedIndex = foundIndex;
        else
            MessageBox.Show("Font was not found.");
    }
}
    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles button1.Click

        If binding1.SupportsSearching <> True Then
            MessageBox.Show("Cannot search the list.")
        Else
            Dim foundIndex As Integer = binding1.Find("Name", textBox1.Text)
            If foundIndex > -1 Then
                listBox1.SelectedIndex = foundIndex
            Else
                MessageBox.Show("Font was not found.")
            End If
        End If

    End Sub
End Class

Commenti

Questo metodo viene in genere usato in casi di data binding complessi per individuare la prima riga in cui il valore del campo specificato dal prop parametro equivale al valore del key parametro

Questo metodo fa semplicemente riferimento alla richiesta al metodo dell'elenco IBindingList.Find sottostante. Ad esempio, se l'origine dati sottostante è un DataSetmetodo , DataTableo DataView, chiama il DataView.IBindingList.Find metodo . Il comportamento di IBindingList.Find, ad esempio il valore restituito se non viene trovato alcun elemento corrispondente, dipende dall'implementazione del metodo nell'elenco sottostante.

Vedi anche

Si applica a

Find(String, Object)

Restituisce l'indice dell'elemento dell'elenco con il nome e il valore di proprietà specificati.

public:
 int Find(System::String ^ propertyName, System::Object ^ key);
public int Find (string propertyName, object key);
member this.Find : string * obj -> int
Public Function Find (propertyName As String, key As Object) As Integer

Parametri

propertyName
String

Nome della proprietà da ricercare.

key
Object

Valore dell'elemento con il propertyName specificato da trovare.

Restituisce

Indice in base zero dell'elemento con il nome e il valore di proprietà specificati.

Eccezioni

L'elenco sottostante non è un oggetto IBindingList con la funzionalità di ricerca implementata.

propertyName non corrisponde a una proprietà presente nell'elenco.

Esempio

Nell'esempio seguente viene illustrato come usare il Find metodo con un DataViewoggetto . Per eseguire questo esempio, incollare il codice in un Windows Form e chiamare PopulateDataViewAndFind dal metodo di gestione eventi o Load costruttore del modulo. Il modulo deve importare gli System.Xml spazi dei nomi e System.IO .

private void PopulateDataViewAndFind()
{
    DataSet set1 = new DataSet();

    // Some xml data to populate the DataSet with.
    string musicXml =
        "<?xml version='1.0' encoding='UTF-8'?>" +
        "<music>" +
        "<recording><artist>Coldplay</artist><cd>X&Y</cd></recording>" +
        "<recording><artist>Dave Matthews</artist><cd>Under the Table and Dreaming</cd></recording>" +
        "<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" +
        "<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" +
        "</music>";

    // Read the xml.
    StringReader reader = new StringReader(musicXml);
    set1.ReadXml(reader);

    // Get a DataView of the table contained in the dataset.
    DataTableCollection tables = set1.Tables;
    DataView view1 = new DataView(tables[0]);

    // Create a DataGridView control and add it to the form.
    DataGridView datagridview1 = new DataGridView();
    datagridview1.AutoGenerateColumns = true;
    this.Controls.Add(datagridview1);

    // Create a BindingSource and set its DataSource property to
    // the DataView.
    BindingSource source1 = new BindingSource();
    source1.DataSource = view1;

    // Set the data source for the DataGridView.
    datagridview1.DataSource = source1;

    // Set the Position property to the results of the Find method.
    int itemFound = source1.Find("artist", "Natalie Merchant");
    source1.Position = itemFound;
}
Private Sub PopulateDataViewAndFind() 
    Dim set1 As New DataSet()
    
    ' Some xml data to populate the DataSet with.
    Dim musicXml As String = "<?xml version='1.0' encoding='UTF-8'?>" & _
        "<music>" & _
        "<recording><artist>Coldplay</artist><cd>X&Y</cd></recording>" & _
        "<recording><artist>Dave Matthews</artist><cd>Under the Table and Dreaming</cd></recording>" & _
        "<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" & _
        "<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" & _
        "</music>"
    
    ' Read the xml.
    Dim reader As New StringReader(musicXml)
    set1.ReadXml(reader)
    
    ' Get a DataView of the table contained in the dataset.
    Dim tables As DataTableCollection = set1.Tables
    Dim view1 As New DataView(tables(0))
    
    ' Create a DataGridView control and add it to the form.
    Dim datagridview1 As New DataGridView()
    datagridview1.AutoGenerateColumns = True
    Me.Controls.Add(datagridview1)
    
    ' Create a BindingSource and set its DataSource property to
    ' the DataView.
    Dim source1 As New BindingSource()
    source1.DataSource = view1
    
    ' Set the data source for the DataGridView.
    datagridview1.DataSource = source1
    
    ' Set the Position property to the results of the Find method.
    Dim itemFound As Integer = source1.Find("artist", "Natalie Merchant")
    source1.Position = itemFound

End Sub

Commenti

Il Find metodo può essere usato solo quando l'elenco sottostante è un IBindingList oggetto con la ricerca implementata. Questo metodo fa semplicemente riferimento alla richiesta al metodo dell'elenco IBindingList.Find sottostante. Ad esempio, se l'origine dati sottostante è un DataSetmetodo , DataTableo DataView, converte propertyName in un PropertyDescriptor e chiama il IBindingList.Find metodo . Il comportamento di Find, ad esempio il valore restituito se non viene trovato alcun elemento corrispondente, dipende dall'implementazione del metodo nell'elenco sottostante.

Il confronto tra nomi delle proprietà è senza distinzione tra maiuscole e minuscole.

Si applica a