BindingSource.Find Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Encontre o item especificado na fonte de dados.
Sobrecargas
| Name | Description |
|---|---|
| Find(PropertyDescriptor, Object) |
Procura o índice do item que tem o descritor de propriedade dado. |
| Find(String, Object) |
Devolve o índice do item na lista com o nome e valor especificados da propriedade. |
Find(PropertyDescriptor, Object)
- Origem:
- BindingSource.cs
- Origem:
- BindingSource.cs
- Origem:
- BindingSource.cs
- Origem:
- BindingSource.cs
- Origem:
- BindingSource.cs
Procura o índice do item que tem o descritor de propriedade dado.
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
Parâmetros
- prop
- PropertyDescriptor
O PropertyDescriptor que procurar.
- key
- Object
O valor de prop para corresponder.
Devoluções
O índice baseado em zero do item que tem o valor dado para PropertyDescriptor.
Implementações
Exceções
A lista subjacente não é do tipo IBindingList.
Exemplos
O exemplo de código seguinte demonstra como usar o Find método. Para o exemplo completo, veja o tópico de visão geral da aula.
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
Observações
Este método é tipicamente usado em casos complexos de ligação de dados para localizar a primeira linha onde o valor do campo especificado pelo prop parâmetro é igual ao valor do key parâmetro
Este método simplesmente encaminha o pedido para o método da IBindingList.Find lista subjacente. Por exemplo, se a fonte de dados subjacente for um DataSet, , ou DataTable, este método chama o DataView métodoDataView.IBindingList.Find. O comportamento de IBindingList.Find, como o valor devolvido se não for encontrado um item correspondente, depende da implementação do método na lista subjacente.
Ver também
Aplica-se a
Find(String, Object)
- Origem:
- BindingSource.cs
- Origem:
- BindingSource.cs
- Origem:
- BindingSource.cs
- Origem:
- BindingSource.cs
- Origem:
- BindingSource.cs
Devolve o índice do item na lista com o nome e valor especificados da propriedade.
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
Parâmetros
- propertyName
- String
O nome da propriedade a procurar.
- key
- Object
O valor do item com o especificado propertyName a encontrar.
Devoluções
O índice baseado em zero do item com o nome e valor especificados da propriedade.
Exceções
A lista subjacente não é uma IBindingList funcionalidade de pesquisa implementada.
propertyName não corresponde a uma propriedade da lista.
Exemplos
O exemplo seguinte mostra como usar o Find método com um DataView. Para executar este exemplo, cole o código num Windows Form e chame PopulateDataViewAndFind do construtor do formulário ou do método de gestão de eventos Load. O seu formulário deve importar os System.Xml namespaces e.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
Observações
O Find método só pode ser usado quando a lista subjacente é um IBindingList com pesquisa implementada. Este método simplesmente encaminha o pedido para o método da IBindingList.Find lista subjacente. Por exemplo, se a fonte de dados subjacente for um DataSet, , ou DataTable, este método converte-se DataView em e propertyName chama o PropertyDescriptor métodoIBindingList.Find. O comportamento de Find, como o valor devolvido se não for encontrado um item correspondente, depende da implementação do método na lista subjacente.
A comparação dos nomes da propriedade é indistinta a maiúsculas e minúsculas.