BindingSource.DataSource 属性   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置连接器绑定到的数据源。
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, setPublic Property DataSource As Object属性值
作为数据源的 Object。 默认值为 null。
示例
下面的代码示例将客户列表分配给DataSourceBindingSource组件的 。 本代码示例是 如何:使用 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 可以设置为多个数据源,包括类型、对象和类型列表。 生成的数据源将以列表的形式公开。 下表显示了一些常见数据源和生成的列表评估。
| 数据源属性 | 列表结果 | 
|---|---|
| null | 对象的空 IBindingList。 添加项会将列表设置为所添加项的类型。 | 
| null具有 DataMember set | 不支持,引发 ArgumentException。 | 
| 非列表类型或类型“T”的对象 | 类型为“T”的空 IBindingList 。 | 
| 数组实例 | IBindingList 包含数组元素。 | 
| IEnumerable 实例 | 一个 IBindingList 包含项的 IEnumerable 。 | 
| 包含类型“T”的列表实例 | IBindingList 包含类型“T”的实例。 | 
此外, DataSource 可以设置为其他列表类型,例如 IListSource 和 ITypedList , BindingSource 将相应地处理它们。 在这种情况下,列表中包含的类型应具有无参数构造函数。
设置数据源时,如果提供的引用包含多个列表或表,则必须将 DataMember 属性设置为指定要绑定到的列表的字符串。 设置此属性将 DataSourceChanged 引发 事件。
注意
如果对 DataSource 属性值进行任何更改,则应在用户界面 (UI) 线程上执行此操作,以确保 UI 反映更改。
属性 DataSource 是 类的默认属性 BindingSource 。