BindingSource.Find 方法

定义

在数据源中查找指定的项。

重载

Find(PropertyDescriptor, Object)

搜索具有指定属性描述符的项索引。

Find(String, Object)

使用指定的属性名和值返回列表中的项的索引。

Find(PropertyDescriptor, Object)

搜索具有指定属性描述符的项索引。

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

参数

prop
PropertyDescriptor

要搜索的 PropertyDescriptor

key
Object

要匹配的 prop 值。

返回

从零开始的项的索引,该项具有 PropertyDescriptor 的给定值。

实现

例外

基础列表不属于类型 IBindingList

示例

下面的代码示例演示如何使用 Find 方法。 有关完整示例,请参阅类概述主题。

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

注解

此方法通常用于复杂的数据绑定情况,用于查找参数指定的 prop 字段值等于参数值 key 的第一行

此方法只是将请求引用到基础列表的 IBindingList.Find 方法。 例如,如果基础数据源是 DataSetDataTableDataView,则此方法将调用 DataView.IBindingList.Find 方法。 IBindingList.Find的行为(如找不到匹配项时返回的值)取决于基础列表中 方法的实现。

另请参阅

适用于

Find(String, Object)

使用指定的属性名和值返回列表中的项的索引。

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

参数

propertyName
String

要搜索的属性的名称。

key
Object

带有指定的要查找的 propertyName 的项的值。

返回

带有指定属性名和值的项的从零开始的索引。

例外

基础列表不是实现了搜索功能的 IBindingList

propertyName 与列表中的属性不匹配。

示例

以下示例演示如何将 Find 方法与 一起使用 DataView。 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的构造函数或Load事件处理方法调用 PopulateDataViewAndFind 。 窗体应导入 System.XmlSystem.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

注解

Find仅当基础列表是实现了搜索的 时IBindingList,才能使用 方法。 此方法只是将请求引用到基础列表的 IBindingList.Find 方法。 例如,如果基础数据源是 DataSet、 或 DataViewDataTable则此方法将转换为 propertyNamePropertyDescriptor 并调用 IBindingList.Find 方法。 Find的行为(如找不到匹配项时返回的值)取决于基础列表中 方法的实现。

属性名称比较不区分大小写。

适用于