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 元件的 。 此程式碼範例是 如何:使用 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 。 新增專案會將清單設定為已新增專案的型別。
nullDataMember set 不支援 ,會 ArgumentException 引發 。
類型為 「T」 的非清單類型或物件 類型為 「T」 的空白 IBindingList
陣列實例 IBindingList 包含陣列元素。
IEnumerable 實例 IBindingList 包含 IEnumerable 專案。
列出包含類型 「T」 的實例 IBindingList 實例,其中包含類型 「T」。

此外, DataSource 也可以設定為其他清單類型,例如 IListSourceITypedList ,而且 BindingSource 會適當地處理它們。 在此情況下,清單中所包含的類型應該具有無參數建構函式。

設定資料來源時,如果提供的參考包含多個清單或資料表,您必須將 DataMember 屬性設定為字串,以指定要系結的清單。 設定這個屬性會 DataSourceChanged 引發 事件。

注意

如果您對屬性值進行任何變更 DataSource ,則應該在使用者介面 (UI) 執行緒上執行這項操作,以確保 UI 反映變更。

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

適用於

另請參閱