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é

IList qui représente la liste, ou null s'il n'y a aucune liste sous-jacente associée à ce BindingSource.

Attributs

Exemples

L’exemple de code suivant illustre les Listmembres , RemoveAtet Count . Pour exécuter cet exemple, collez le code dans un formulaire qui contient un BindingSource nommé BindingSource1, deux étiquettes nommées label1 et label2, et un 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. Idéalement, la List propriété doit être définie sur un général IList. Toutefois, il peut parfois être nécessaire de caster 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 ArrayList vide.
DataSource est null, mais DataMember n’est pas null Aucun; une tentative d’obtention de l’objet List lève un ArgumentException.
Un Array instance Élément Array.
Un IListSource instance Valeur de retour d’un appel à la GetList méthode de cette IListSource instance.
Un IBindingList instance Élément IBindingList.
Un IList instance Élément IList.
Non-instanceIList de type « T » BindingList<T> avec un élément.
Un ICustomTypeDescriptor instance avec ArrayList un élément.
IEnumerable. ArrayList avec les éléments copiés.
Type Array avec DataMember le type d’élément « T » BindingList<T>
Type qui représente un IListSource ouITypedList Un instance créé par un appel à la CreateInstance(Type) méthode de la Activator classe . Un NotSupportedException peut être jeté.
Type IList avec DataMember le type d’élément « T »

- ou -

Un type autre queIList
BindingList<T>
Le type ICustomTypeDescriptor Aucun; une tentative d’obtention de l’objet List lève un NotSupportedException.

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

S’applique à

Voir aussi