BindingSource.List Propriedade

Definição

Obtém a lista a que o conector está ligado.

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 de Propriedade

Um IList que representa a lista, ou null se não houver uma lista subjacente associada a esta BindingSource.

Atributos

Exemplos

O exemplo de código seguinte demonstra os List, RemoveAt, e Count os membros. Para executar este exemplo, cole o código numa forma que contenha um BindingSource nome BindingSource1, duas etiquetas nomeadas label1 e label2, e um botão chamado button1. Associe o button1_Click método ao Click evento para button1. Visual Basic utilizadores terão de 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

Observações

A BindingSource turma lida uniformemente com diferentes fontes de dados. Idealmente, a List propriedade deve ser definida para um geral IList. No entanto, por vezes pode ser necessário atribuir esta propriedade a um tipo mais específico. A tabela seguinte 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 vazio ArrayList.
DataSource é null, mas DataMember não é null Nenhum; Uma tentativa de obter a List vontade lançar um ArgumentException.
Uma Array instância Um Array.
Uma IListSource instância O valor de retorno de uma chamada para o GetList método desta IListSource instância.
Uma IBindingList instância Um IBindingList.
Uma IList instância Um IList.
Uma não-instânciaIList do tipo "T" A BindingList<T> com um elemento.
Uma ICustomTypeDescriptor instância E ArrayList com um elemento.
Um IEnumerable E ArrayList com os elementos copiados.
O Array tipo com DataMember um tipo de item "T" Um BindingList<T>.
A Type que representa um IListSource ou ITypedList Uma instância criada por uma chamada ao CreateInstance(Type) método da Activator classe. A NotSupportedException pode ser lançado.
O IList tipo com DataMember um tipo de item "T"

-ou-

Um não-tipoIList
Um BindingList<T>.
O ICustomTypeDescriptor tipo Nenhum; Uma tentativa de obter a List vontade lançar um NotSupportedException.

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

Aplica-se a

Ver também