Partager via


BindingSource.List Propriété

Définition

Obtient la liste à laquelle le connecteur est lié.

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

Valeur de propriété

Qui IList représente la liste, ou null s’il n’existe aucune liste sous-jacente associée à ce BindingSource.

Attributs

Exemples

L’exemple de code suivant illustre les membres RemoveAtet Count les Listmembres. Pour exécuter cet exemple, collez le code dans un formulaire qui contient un BindingSource nom BindingSource1, deux étiquettes nommées label1 et label2un bouton nommé button1. Associez la button1_Click méthode à l’événement Click pour button1. Les utilisateurs Visual Basic devront ajouter une référence à 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

Remarques

La BindingSource classe gère uniformément différentes sources de données. Dans l’idéal, la List propriété doit être définie sur une valeur générale IList. Toutefois, il peut parfois être nécessaire de convertir cette propriété en un type plus spécifique. Le tableau suivant montre le type de liste sous-jacent, qui dépend du type ou de la valeur de la source de données.

Type de source de données Description de la liste sous-jacente
DataSource et DataMember sont null ArrayListVide .
DataSource est null, mais DataMember n’est pas null Aucun; une tentative d’obtention de la List levée d’un ArgumentException.
Instance Array Élément Array.
Instance IListSource Valeur de retour d’un appel à la GetList méthode de cette IListSource instance.
Instance IBindingList Élément IBindingList.
Instance IList Élément IList.
Instance non-deIList type « T » Avec BindingList<T> un seul élément.
Instance ICustomTypeDescriptor Avec ArrayList un élément.
Un IEnumerable Avec ArrayList les éléments copiés.
Type Array avec DataMember le type d’élément « T » Un BindingList<T>.
Qui Type représente un IListSource ou ITypedList Instance créée par un appel à la CreateInstance(Type) méthode de la Activator classe. A NotSupportedException peut-être levée.
Type IList avec DataMember le type d’élément « T »

- ou -

Un type non-typeIList
Un BindingList<T>.
Le type ICustomTypeDescriptor Aucun; une tentative d’obtention de la List levée d’un NotSupportedException.

Si le type récupéré est l’interfaceIList, la collection sous-jacente peut être plus complexe, telle qu’une ou DataView une ArrayList classe.

S’applique à

Voir aussi