BindingSource.List 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會取得連接器綁定的清單。
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 代表該列表,或者 null 如果沒有與此 BindingSource列表相關聯的底層列表。
- 屬性
範例
以下程式碼範例展示了 List、 RemoveAt和 Count 成員。 要執行此範例,將程式碼貼入包含BindingSource一個名為 BindingSource1、 的兩個標籤 label2label1 和 ,以及一個名為 button1的按鈕 的表單中。 將方法 button1_Click 與 Click 事件 button1關聯。 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。 然而,有時可能需要將此屬性歸類為更特定的類型。 下表顯示底層清單類型,取決於資料來源的類型或值。
| 資料來源類型 | 底層清單描述 |
|---|---|
DataSource 且 DataMember 是 null |
一個空洞的 ArrayList。 |
DataSource 是 null,但 DataMember 不是 null |
沒有;嘗試讓 Will List 擲出 ArgumentException。 |
| 一個 Array 例子 | Array。 |
| 一個 IListSource 例子 | 呼叫該 GetList 實例方法 IListSource 的回傳值。 |
| 一個 IBindingList 例子 | IBindingList。 |
| 一個 IList 例子 | IList。 |
| 非IList 類型「T」的實例 | BindingList<T> A,只有一個元素。 |
| 一個 ICustomTypeDescriptor 例子 | 一個 ArrayList 只有一個元素的。 |
| 一個 IEnumerable | 還有 ArrayList 元素被複製過來。 |
| Array項目DataMember類型為「T」的類型 | BindingList<T>。 |
| Type A 代表一個IListSource或ITypedList | 一個由呼叫類別 CreateInstance(Type) 方法 Activator 所建立的實例。 NotSupportedException A可以被擲出。 |
|
IList項目DataMember類型為「T」的類型 -或- 非IList 類型 |
BindingList<T>。 |
| ICustomTypeDescriptor 類型 | 沒有;嘗試讓 Will List 擲出 NotSupportedException。 |
如果檢索的型別是介面, IList 底層集合可能會更複雜,例如一個 ArrayList or DataView 類別。