BindingSource.List Egenskap

Definition

Hämtar listan som anslutningsappen är bunden till.

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

Egenskapsvärde

En IList som representerar listan, eller null om det inte finns någon underliggande lista associerad med den här BindingSource.

Attribut

Exempel

I följande kodexempel visas Listmedlemmarna , RemoveAtoch Count . Om du vill köra det här exemplet klistrar du in koden i ett formulär som innehåller ett BindingSource med namnet BindingSource1, två etiketter med namnet label1 och label2och en knapp med namnet button1. button1_Click Associera metoden med Click händelsen för button1. Visual Basic användare måste lägga till en referens till 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

Kommentarer

Klassen BindingSource hanterar olika datakällor på ett enhetligt sätt. Helst bör egenskapen List vara inställd på en allmän IList. Ibland kan det dock vara nödvändigt att omvandla den här egenskapen till en mer specifik typ. I följande tabell visas den underliggande listtypen, som beror på datakällans typ eller värde.

Typ av datakälla Beskrivning av underliggande lista
DataSource och DataMember är null En tom ArrayList.
DataSource är null, men DataMember är inte null Ingen; ett försök att hämta List kommer att kasta en ArgumentException.
En Array instans En Array.
En IListSource instans Returvärdet från ett anrop till metoden för den här GetList instansenIListSource.
En IBindingList instans En IBindingList.
En IList instans En IList.
En icke-instansIList av typen "T" Ett BindingList<T> med ett element.
En ICustomTypeDescriptor instans Ett ArrayList med ett element.
En IEnumerable Ett ArrayList med elementen kopierade över.
Typen Array med DataMember objekttypen "T" En BindingList<T>.
En Type som representerar en IListSource eller ITypedList En instans som skapats av ett anrop till CreateInstance(Type) klassens Activator metod. En NotSupportedException kan kastas.
Typen IList med DataMember objekttypen "T"

-eller-

En icke-typIList
En BindingList<T>.
Typ ICustomTypeDescriptor Ingen; ett försök att hämta List kommer att kasta en NotSupportedException.

Om den typ som hämtas är IList gränssnittet kan den underliggande samlingen vara mer komplex, till exempel en ArrayList eller DataView en klass.

Gäller för

Se även