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和RemoveAtCount成员。 若要运行此示例,请将代码粘贴到包含BindingSource命名标签、两label1个标签BindingSource1和label2一个名为按钮的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和 are DataMembernull |
ArrayList空 。 |
DataSource 是 null,但 DataMember 不是 null |
没有;尝试获取 List will 将引发一个 ArgumentException。 |
| 实例Array | Array。 |
| 实例IListSource | 从调用 GetList 此 IListSource 实例的方法返回值。 |
| 实例IBindingList | IBindingList。 |
| 实例IList | IList。 |
| 类型为“T”的非IList 实例 | 具有一个元素的 A BindingList<T> 。 |
| 实例ICustomTypeDescriptor | 具有 ArrayList 一个元素。 |
| An IEnumerable | 复制了元素的元素 ArrayList 。 |
| Array项DataMember类型为“T”的类型 | 一个 BindingList<T>。 |
| Type表示IListSource或ITypedList | 通过调用 CreateInstance(Type) 类的方法 Activator 创建的实例。 可能会引发 A NotSupportedException 。 |
|
IList项DataMember类型为“T”的类型 -或- 非IList 类型 |
一个 BindingList<T>。 |
| ICustomTypeDescriptor 类型 | 没有;尝试获取 List will 将引发一个 NotSupportedException。 |
如果检索的类型是 IList 接口,则基础集合可能更为复杂,例如 ArrayList 或 DataView 类。