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 Form 中,並從表單的建構函式或 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 的 方法。 例如,如果基礎資料來源是 、 或 ,這個方法會 PropertyDescriptorpropertyName 轉換成 ,並呼叫 IBindingList.Find 方法。 DataViewDataTableDataSet Find的行為,例如找不到相符專案時所傳回的值,取決於基礎清單中的 方法實作。

屬性名稱比較不區分大小寫。

適用於