BindingSource.Find 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在資料來源中找到指定的項目。
多載
| 名稱 | Description |
|---|---|
| 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 方法。 完整範例請參見課程總覽主題。
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.");
}
}
}
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 請求指向底層清單的方法。 例如,若底層資料來源為 DataSet、 DataTable或 DataView,此方法呼叫該 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 表單,並呼叫 PopulateDataViewAndFind 表單的建構子或 Load 事件處理方法。 你的表單應該會匯入 System.Xml 和 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
備註
Find此方法僅在底層清單為 且IBindingList已實作搜尋時使用。 此方法僅將 IBindingList.Find 請求指向底層清單的方法。 例如,若底層資料來源為 DataSet、 DataTable或 DataView,此方法會 propertyName 轉換成 並 PropertyDescriptor 呼叫該 IBindingList.Find 方法。 的行為 Find,例如若未找到匹配項目時回傳的值,取決於該方法在底層清單中的實作。
房產名稱比較不區分大小寫。