BindingSource.Find メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
データ ソース内の指定した項目を検索します。
オーバーロード
| 名前 | 説明 |
|---|---|
| Find(PropertyDescriptor, Object) |
指定されたプロパティ記述子を持つ項目のインデックスを検索します。 |
| Find(String, Object) |
指定したプロパティ名と値を持つリスト内の項目のインデックスを返します。 |
Find(PropertyDescriptor, Object)
- ソース:
- BindingSource.cs
- ソース:
- BindingSource.cs
- ソース:
- BindingSource.cs
- ソース:
- BindingSource.cs
- ソース:
- BindingSource.cs
指定されたプロパティ記述子を持つ項目のインデックスを検索します。
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の指定した値を持つ項目の 0 から始まるインデックス。
実装
例外
基になるリストが 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)
- ソース:
- BindingSource.cs
- ソース:
- BindingSource.cs
- ソース:
- BindingSource.cs
- ソース:
- BindingSource.cs
- ソース:
- BindingSource.cs
指定したプロパティ名と値を持つリスト内の項目のインデックスを返します。
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 を持つ項目の値。
返品
指定したプロパティ名と値を持つ項目の 0 から始まるインデックス。
例外
基になるリストは、検索機能が実装された 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の動作は、基になるリスト内のメソッドの実装によって異なります。
プロパティ名の比較では、大文字と小文字が区別されません。