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, oppure null se non è presente alcun elenco sottostante associato a questo oggetto BindingSource.

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 nome , due etichette denominate e label2e un BindingSource pulsante denominato label1button1.BindingSource1 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 diverse origini dati. 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 elenco sottostante
DataSource e DataMember sono null Oggetto ArrayList vuoto.
DataSource è null, ma DataMember non è null Nessuno; un tentativo di ottenere l'oggetto List genererà un ArgumentExceptionoggetto .
Un'istanza Array Oggetto Array.
Un'istanza IListSource Valore restituito da una chiamata al GetList metodo di questa IListSource istanza.
Un'istanza IBindingList Oggetto IBindingList.
Un'istanza IList Oggetto IList.
IList Istanza non di tipo "T" Oggetto BindingList<T> con un elemento.
Un'istanza ICustomTypeDescriptor Oggetto ArrayList con un elemento.
Oggetto IEnumerable. Oggetto ArrayList con gli elementi copiati.
Tipo Array con DataMember tipo di elemento "T" Oggetto BindingList<T>.
Oggetto Type che rappresenta un IListSource oggetto o ITypedList Istanza creata da una chiamata al CreateInstance(Type) metodo della Activator classe. Può essere generata una NotSupportedException classe.
Tipo IList con DataMember tipo di elemento "T"

-oppure-

Tipo nonIList
Oggetto BindingList<T>.
Tipo ICustomTypeDescriptor Nessuno; un tentativo di ottenere l'oggetto List genererà un NotSupportedExceptionoggetto .

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