語言

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。

範例

以下程式碼範例將客戶清單指派到元件DataSource的 。BindingSource 此程式碼範例是「 如何使用 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 IEnumerable 執行個體 安 IBindingList 納物品 IEnumerable 。
包含類型參數「T」的清單執行個體 IBindingList 包含類型「T」的實例。

此外, DataSource 可以設定為其他列表類型,如 IListSource 和 ITypedList ,且會 BindingSource 適當處理它們。 在這種情況下,清單中包含的型態應該有一個無參數的建構子。

在設定資料來源時,如果所提供的參考包含多個清單或資料表,你必須將屬性設定 DataMember 為一個字串,指定要綁定的清單。 設定此屬性會提升事件。DataSourceChanged

Note

如果你對屬性值做任何更改 DataSource ,應該在使用者介面(UI)執行緒中進行,以確保介面反映變更。

屬性 DataSource 是該 BindingSource 類別的預設屬性。

適用於

另請參閱