BindingSource.List Propriedade

Definição

Obtém a lista a qual o conector está associado.

public:
 property System::Collections::IList ^ List { System::Collections::IList ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Collections.IList List { get; }
[<System.ComponentModel.Browsable(false)>]
member this.List : System.Collections.IList
Public ReadOnly Property List As IList

Valor da propriedade

Um IList que representa a lista ou null se não houver nenhuma lista subjacente associada a esse BindingSource.

Atributos

Exemplos

O exemplo de código a seguir demonstra os Listmembros , RemoveAte Count . Para executar este exemplo, cole o código em um formulário que contém um BindingSource chamado BindingSource1, dois rótulos chamados label1 e label2e um botão chamado button1. Associe o button1_Click método ao Click evento para button1. Os usuários do Visual Basic precisarão adicionar uma referência a System.Data.dll.

private void button1_Click(object sender, EventArgs e)
{
    // Create the connection string, data adapter and data table.
    SqlConnection connectionString =
         new SqlConnection("Initial Catalog=Northwind;" +
         "Data Source=localhost;Integrated Security=SSPI;");
    SqlDataAdapter customersTableAdapter =
        new SqlDataAdapter("Select * from Customers", connectionString);
    DataTable customerTable = new DataTable();

    // Fill the adapter with the contents of the customer table.
    customersTableAdapter.Fill(customerTable);

    // Set data source for BindingSource1.
    BindingSource1.DataSource = customerTable;

    // Set the label text to the number of items in the collection before
    // an item is removed.
    label1.Text = "Starting count: " + BindingSource1.Count.ToString();

    // Access the List property and remove an item.
    BindingSource1.List.RemoveAt(4);

    // Remove an item directly from the BindingSource. 
    // This is equivalent to the previous line of code.
    BindingSource1.RemoveAt(4);

    // Show the new count.
    label2.Text = "Count after removal: " + BindingSource1.Count.ToString();
}
    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles button1.Click

        ' Create the connection string, data adapter and data table.
        Dim connectionString As New SqlConnection("Initial Catalog=Northwind;" & _
            "Data Source=localhost;Integrated Security=SSPI;")
        Dim customersTableAdapter As New SqlDataAdapter("Select * from Customers", _
            connectionString)
        Dim customerTable As New DataTable()

        ' Fill the adapter with the contents of the customer table.
        customersTableAdapter.Fill(customerTable)

        ' Set data source for BindingSource1.
        BindingSource1.DataSource = customerTable

        ' Set the label text to the number of items in the collection before
        ' an item is removed.
        label1.Text = "Starting count: " + BindingSource1.Count.ToString()

        ' Access the List property and remove an item.
        BindingSource1.List.RemoveAt(4)

        ' Remove an item directly from the BindingSource. 
        ' This is equivalent to the previous line of code.
        BindingSource1.RemoveAt(4)

        ' Show the new count.
        label2.Text = "Count after removal: " + BindingSource1.Count.ToString()

    End Sub
End Class

Comentários

A BindingSource classe manipula uniformemente diferentes fontes de dados. O ideal é que a List propriedade seja definida como um geral IList. No entanto, às vezes, pode ser necessário converter essa propriedade em um tipo mais específico. A tabela a seguir mostra o tipo de lista subjacente, que depende do tipo ou valor da fonte de dados.

Tipo de fonte de dados Descrição da lista subjacente
DataSource e DataMember são null Um ArrayList vazio.
DataSource é null, mas DataMember não é null Nenhum; uma tentativa de obter o List lançará um ArgumentException.
Uma Array instância Um Array.
Uma IListSource instância O valor retornado de uma chamada para o GetList método dessa IListSource instância.
Uma IBindingList instância Um IBindingList.
Uma IList instância Um IList.
IList Uma instância diferente do tipo "T" Um BindingList<T> com um elemento .
Uma ICustomTypeDescriptor instância Um ArrayList com um elemento .
Um IEnumerable Um ArrayList com os elementos copiados.
O Array tipo com DataMember o tipo de item "T" Um BindingList<T>.
Um Type que representa um IListSource ou ITypedList Uma instância criada por uma chamada para o CreateInstance(Type) método da Activator classe . Um NotSupportedException pode ser lançado.
O IList tipo com DataMember o tipo de item "T"

- ou -

Um tipo nãoIList tipo
Um BindingList<T>.
O tipo ICustomTypeDescriptor. Nenhum; uma tentativa de obter o List lançará um NotSupportedException.

Se o tipo recuperado for a IList interface, a coleção subjacente poderá ser mais complexa, como uma ArrayList classe ou DataView .

Aplica-se a

Confira também