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, set
Public Property DataSource As Object
屬性值
而 Object 這也是一個資料來源。 預設值為 null。
範例
以下程式碼範例將客戶清單指派到元件BindingSource的 。DataSource 此程式碼範例是「 如何使用 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 set |
沒有支持,但會加 ArgumentException薪。 |
| 非列表型別或類型「T」的物件 | T型為空 IBindingList 。 |
| 陣列實例 | IBindingList 包含陣列元素。 |
| IEnumerable 實例 | 安 IBindingList 納物品 IEnumerable 。 |
| 包含類型「T」的清單實例 | IBindingList 包含類型「T」的實例。 |
此外, DataSource 可以設定為其他列表類型,如 IListSource 和 ITypedList ,且會 BindingSource 適當處理它們。 在這種情況下,清單中包含的型態應該有一個無參數的建構子。
在設定資料來源時,如果所提供的參考包含多個清單或資料表,你必須將屬性設定 DataMember 為一個字串,指定要綁定的清單。 設定此屬性會提升事件。DataSourceChanged
備註
如果你對屬性值做任何更改 DataSource ,應該在使用者介面(UI)執行緒中進行,以確保介面反映變更。
屬性 DataSource 是該 BindingSource 類別的預設屬性。