BindingSource.List 屬性

定義

取得連接器要繫結至其中的清單。

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

屬性值

表示此清單的 IList;如果沒有與此 BindingSource 關聯的基礎清單,則為 null

屬性

範例

下列程式碼範例示範 ListRemoveAtCount 成員。 若要執行此範例,請將程式碼貼到表單中,其中包含名為 的 、名為 和 的兩個 BindingSource 標籤,以及名為 的 button1 按鈕。 label2label1BindingSource1button1_Click 方法與 的 Clickbutton1 事件產生關聯。 Visual Basic 使用者必須新增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

備註

類別會 BindingSource 統一處理不同的資料來源。 在理想情況下, List 屬性應該設定為一般 IList 。 不過,有時候可能需要將這個屬性轉換成更特定的類型。 下表顯示基礎清單類型,這取決於資料來源的類型或值。

資料來源類型 基礎清單描述
DataSourceDataMembernull 空的 ArrayList
DataSourcenull ,但 DataMember 不是 null 沒有;嘗試取得 List 將會擲 ArgumentException 回 。
Array實例 Array
IListSource實例 從呼叫 GetList 這個 IListSource 實例之 方法的傳回值。
IBindingList實例 IBindingList
IList實例 IList
類型為 「T」 的非 IList 實例 BindingList<T>具有一個元素的 。
ICustomTypeDescriptor實例 ArrayList具有一個元素的 。
IEnumerable ArrayList 其中已複製專案。
類型 ArrayDataMember 「T」 的專案類型 BindingList<T>
Type,表示 IListSourceITypedList 呼叫 類別之 方法所 CreateInstance(Type) 建立的 Activator 實例。 NotSupportedException可能會擲回 。
類型 IListDataMember 「T」 的專案類型

-或-

IList 類型
BindingList<T>
ICustomTypeDescriptor 類型 沒有;嘗試取得 List 將會擲 NotSupportedException 回 。

如果擷取的類型是 IList 介面,基礎集合可能更為複雜,例如 ArrayListDataView 類別。

適用於

另請參閱