Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
BindingSource Class
Find Method
 Find Method (String, Object)
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
BindingSource..::.Find Method (String, Object)

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

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Declaration)
Public Function Find ( _
    propertyName As String, _
    key As Object _
) As Integer
Visual Basic (Usage)
Dim instance As BindingSource
Dim propertyName As String
Dim key As Object
Dim returnValue As Integer

returnValue = instance.Find(propertyName, _
    key)
C#
public int Find(
    string propertyName,
    Object key
)
Visual C++
public:
int Find(
    String^ propertyName, 
    Object^ key
)
JScript
public function Find(
    propertyName : String, 
    key : Object
) : int

Parameters

propertyName
Type: System..::.String
The name of the property to search for.
key
Type: System..::.Object
The value of the item with the specified propertyName to find.

Return Value

Type: System..::.Int32
The zero-based index of the item with the specified property name and value.
ExceptionCondition
InvalidOperationException

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

ArgumentException

propertyName does not match a property in the list.

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.

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.

Visual Basic
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&amp;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

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&amp;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;
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker