BindingSource.DataSource プロパティ

定義

コネクタのバインド先のデータ ソースを取得または設定します。

public:
 property System::Object ^ DataSource { System::Object ^ get(); void set(System::Object ^ value); };
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 インスタンス IBindingList項目を含むIEnumerable
型 "T" を含むリスト インスタンス IBindingList 型 "T" を含むインスタンス。

さらに、DataSourceIListSourceITypedListなどの他のリストの種類に設定でき、BindingSourceで適切に処理されます。 この場合、リストに含まれる型にはパラメーターなしのコンストラクターが必要です。

データ ソースを設定するときに、指定された参照に複数のリストまたはテーブルが含まれている場合は、バインド先のリストを指定する文字列に DataMember プロパティを設定する必要があります。 このプロパティを設定すると、 DataSourceChanged イベントが発生します。

Note

DataSourceプロパティ値に変更を加えた場合は、ユーザー インターフェイス (UI) スレッドで変更を行って、UI に変更が反映されるようにする必要があります。

DataSource プロパティは、BindingSource クラスの既定のプロパティです。

適用対象

こちらもご覧ください