语言

BindingSource.DataSource 属性

定义

获取或设置连接器绑定到的数据源。

public:
 property System::Object ^ DataSource { System::Object ^ get(); void set(System::Object ^ value); };
public object DataSource { get; set; }
public object? DataSource { get; set; }
member this.DataSource : obj with get, set
Public Property DataSource As Object

属性值

充当数据源的一 Object 个。 默认值为 null。

示例

下面的代码示例将客户DataSourceBindingSource列表分配给组件。 此代码示例是 How to: Raise Change Notifications Using the BindingSource ResetItem 方法提供的较大示例的一部分。

void Form1_Load( Object^ /*sender*/, EventArgs^ /*e*/ )
{
   // Create and populate the list of DemoCustomer objects
   // which will supply data to the DataGridView.
   List< DemoCustomer^ >^ customerList = gcnew List< DemoCustomer^ >;
   customerList->Add( DemoCustomer::CreateNewCustomer() );
   customerList->Add( DemoCustomer::CreateNewCustomer() );
   customerList->Add( DemoCustomer::CreateNewCustomer() );
   
   // Bind the list to the BindingSource.
   this->customersBindingSource->DataSource = customerList;
   
   // Attach the BindingSource to the DataGridView.
   this->customersDataGridView->DataSource =
      this->customersBindingSource;
}
private void Form1_Load(System.Object sender, System.EventArgs e)
{
    // Create and populate the list of DemoCustomer objects
    // which will supply data to the DataGridView.
    List<DemoCustomer> customerList = new List<DemoCustomer>();
    customerList.Add(DemoCustomer.CreateNewCustomer());
    customerList.Add(DemoCustomer.CreateNewCustomer());
    customerList.Add(DemoCustomer.CreateNewCustomer());

    // Bind the list to the BindingSource.
    this.customersBindingSource.DataSource = customerList;

    // Attach the BindingSource to the DataGridView.
    this.customersDataGridView.DataSource = 
        this.customersBindingSource;
}
Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Me.Load
    ' Create and populate the list of DemoCustomer objects
    ' which will supply data to the DataGridView.
    Dim customerList As List(Of DemoCustomer) = _
    New List(Of DemoCustomer)
    customerList.Add(DemoCustomer.CreateNewCustomer())
    customerList.Add(DemoCustomer.CreateNewCustomer())
    customerList.Add(DemoCustomer.CreateNewCustomer())

    ' Bind the list to the BindingSource.
    Me.customersBindingSource.DataSource = customerList

    ' Attach the BindingSource to the DataGridView.
    Me.customersDataGridView.DataSource = Me.customersBindingSource
End Sub

注解

该 DataSource 属性可以设置为多个数据源,包括类型、对象和类型列表。 生成的数据源将作为列表公开。 下表显示了一些常见的数据源和生成的列表评估。

DataSource 属性 显示结果
null 空的IBindingList对象。 添加项会将列表设置为所添加项的类型。
null 与 DataMember 设置 不支持,引发 ArgumentException。
类型为“T”的非列表类型或对象 类型为“T”的空 IBindingList 。
数组实例 IBindingList 包含数组元素。
IEnumerable 实例 包含IBindingList项的项IEnumerable。
包含类型“T”的列表实例 IBindingList 包含类型“T”的实例。

此外, DataSource 可以设置为其他列表类型,例如 IListSource , ITypedList 并且 BindingSource 将相应地处理它们。 在这种情况下,列表中所包含的类型应具有无参数构造函数。

设置数据源时,如果提供的引用包含多个列表或表,则必须将该 DataMember 属性设置为指定要绑定到的列表的字符串。 设置此属性将 DataSourceChanged 引发事件。

注释

如果对 DataSource 属性值进行任何更改,则应在用户界面(UI)线程上执行此操作,以确保 UI 反映更改。

该 DataSource 属性是类的默认属性 BindingSource 。

适用于

另请参阅