Condividi tramite


BindingSource.List Proprietà

Definizione

Ottiene l'elenco a cui è associato il connettore.

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

Valore della proprietà

Oggetto IList che rappresenta l'elenco o null se non è presente alcun elenco sottostante associato a questo BindingSourceoggetto .

Attributi

Esempio

Nell'esempio di codice seguente vengono illustrati i Listmembri , RemoveAte Count . Per eseguire questo esempio, incollare il codice in un modulo contenente un BindingSource oggetto denominato , due etichette denominate BindingSource1label1 e label2e un pulsante denominato button1. Associare il button1_Click metodo all'evento Click per button1. Gli utenti di Visual Basic dovranno aggiungere un riferimento 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

Commenti

La BindingSource classe gestisce in modo uniforme origini dati diverse. Idealmente, la List proprietà deve essere impostata su un oggetto generale IList. Tuttavia, a volte potrebbe essere necessario eseguire il cast di questa proprietà in un tipo più specifico. Nella tabella seguente viene illustrato il tipo di elenco sottostante, che dipende dal tipo o dal valore dell'origine dati.

Tipo di origine dati Descrizione dell'elenco sottostante
DataSource e DataMember sono null Oggetto vuoto ArrayList.
DataSource è null, ma DataMember non è null Nessuno; un tentativo di ottenere l'oggetto List genererà un'eccezione ArgumentException.
Istanza di Array Un oggetto Array.
Istanza di IListSource Valore restituito da una chiamata al GetList metodo di questa IListSource istanza.
Istanza di IBindingList Un oggetto IBindingList.
Istanza di IList Un oggetto IList.
IList Istanza non di tipo "T" Oggetto BindingList<T> con un elemento .
Istanza di ICustomTypeDescriptor Oggetto ArrayList con un elemento .
Un IEnumerable Oggetto ArrayList con gli elementi copiati.
Tipo Array con DataMember tipo di elemento "T" Un oggetto BindingList<T>.
Oggetto Type che rappresenta un oggetto IListSource o ITypedList Istanza creata da una chiamata al CreateInstance(Type) metodo della Activator classe . È possibile che venga generata un'eccezione NotSupportedException .
Tipo IList con DataMember tipo di elemento "T"

oppure

Un tipo diversoIList da
Un oggetto BindingList<T>.
Il tipo ICustomTypeDescriptor Nessuno; un tentativo di ottenere l'oggetto List genererà un'eccezione NotSupportedException.

Se il tipo recuperato è l'interfaccia IList , la raccolta sottostante può essere più complessa, ad esempio una ArrayList classe o DataView .

Si applica a

Vedi anche