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 成员。 若要运行此示例,请将代码粘贴到一个窗体中,其中包含一BindingSource1个名为 、两个名为 label1BindingSourcelabel2的标签以及一个名为 的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。 但是,有时可能需要将此属性强制转换为更具体的类型。 下表显示了基础列表类型,具体取决于数据源的类型或值。

数据源类型 基础列表说明
DataSourceDataMembernull 一个空 ArrayList
DataSourcenull,但 DataMember 不是 null 没有;尝试获取 将 List 引发 ArgumentException
实例Array Array
实例IListSource 调用此IListSource实例的 方法的返回值GetList
实例IBindingList IBindingList
实例IList IList
类型为“T”的非IList 实例 BindingList<T>具有一个元素的 。
实例ICustomTypeDescriptor ArrayList具有一个元素的 。
一个 IEnumerable 一个 ArrayList ,其中包含复制的元素。
ArrayDataMember类型为“T”的类型 BindingList<T>
TypeIListSource ,它表示 或 ITypedList 通过调用 CreateInstance(Type) 类的 Activator 方法创建的实例。 NotSupportedException可能会引发 。
IListDataMember类型为“T”的类型

- 或 -

IList 类型
BindingList<T>
ICustomTypeDescriptor 类型 没有;尝试获取 将 List 引发 NotSupportedException

如果检索到的类型是 IList 接口,则基础集合可能更复杂, ArrayList 例如 或 DataView 类。

适用于

另请参阅