BindingSource.Find Method

Definition

Find the specified item in the data source.

Overloads

Find(PropertyDescriptor, Object)

Searches for the index of the item that has the given property descriptor.

Find(String, Object)

Returns the index of the item in the list with the specified property name and value.

Find(PropertyDescriptor, Object)

Source:
BindingSource.cs
Source:
BindingSource.cs
Source:
BindingSource.cs

Searches for the index of the item that has the given property descriptor.

C#
public virtual int Find(System.ComponentModel.PropertyDescriptor prop, object key);

Parameters

prop
PropertyDescriptor

The PropertyDescriptor to search for.

key
Object

The value of prop to match.

Returns

The zero-based index of the item that has the given value for PropertyDescriptor.

Implements

Exceptions

The underlying list is not of type IBindingList.

Examples

The following code example demonstrates how to use the Find method. For the complete example see the class overview topic.

C#
private void button1_Click(object sender, EventArgs e)
{
    if (!binding1.SupportsSearching)
    {
        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.");
    }
}

Remarks

This method is typically used in complex data-binding cases to locate the first row where the value of the field specified by the prop parameter equals the value of the key parameter

This method simply refers the request to the underlying list's IBindingList.Find method. For example, if the underlying data source is a DataSet, DataTable, or DataView, this method calls the DataView.IBindingList.Find method. The behavior of IBindingList.Find, such as the value returned if no matching item is found, depends on the implementation of the method in the underlying list.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.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
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Find(String, Object)

Source:
BindingSource.cs
Source:
BindingSource.cs
Source:
BindingSource.cs

Returns the index of the item in the list with the specified property name and value.

C#
public int Find(string propertyName, object key);

Parameters

propertyName
String

The name of the property to search for.

key
Object

The value of the item with the specified propertyName to find.

Returns

The zero-based index of the item with the specified property name and value.

Exceptions

The underlying list is not a IBindingList with searching functionality implemented.

propertyName does not match a property in the list.

Examples

The following example shows how to use the Find method with a DataView. To run this example, paste the code into a Windows Form and call PopulateDataViewAndFind from the form's constructor or Load event-handling method. Your form should import the System.Xml and System.IO namespaces.

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

Remarks

The Find method can only be used when the underlying list is an IBindingList with searching implemented. This method simply refers the request to the underlying list's IBindingList.Find method. For example, if the underlying data source is a DataSet, DataTable, or DataView, this method converts propertyName to a PropertyDescriptor and calls the IBindingList.Find method. The behavior of Find, such as the value returned if no matching item is found, depends on the implementation of the method in the underlying list.

The property name comparison is case-insensitive.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.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
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10