DataGrid.DataSource プロパティ

定義

グリッドでデータが表示される対象のデータ ソースを取得または設定します。

public:
 property System::Object ^ DataSource { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public object DataSource { get; set; }
public object DataSource { get; set; }
[<System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")>]
member this.DataSource : obj with get, set
member this.DataSource : obj with get, set
Public Property DataSource As Object

プロパティ値

Object

データ ソースとして機能するオブジェクト。

属性

次のコード例は、a と a のDataSource両方にバインドSystem.Windows.Forms.DataGridする 、および必要なDataMember場合に 、 を設定する方法をDataSetDataViewしています。 この例では、. から System.Windows.Forms.DataGridデータ ソースを返す方法も示しています。

private:
   void BindToDataView( DataGrid^ myGrid )
   {
      // Create a DataView using the DataTable.
      DataTable^ myTable = gcnew DataTable( "Suppliers" );
      // Insert code to create and populate columns.
      DataView^ myDataView = gcnew DataView( myTable );
      myGrid->DataSource = myDataView;
   }

   void BindToDataSet( DataGrid^ myGrid )
   {
      // Create a DataSet.
      DataSet^ myDataSet = gcnew DataSet( "myDataSet" );
      // Insert code to populate DataSet with several tables.
      myGrid->DataSource = myDataSet;
      // Use the DataMember property to specify the DataTable.
      myGrid->DataMember = "Suppliers";
   }

   DataView^ GetDataViewFromDataSource()
   {
      // Create a DataTable variable, and set it to the DataSource.
      DataView^ myDataView;
      myDataView = (DataView^)(dataGrid1->DataSource);
      return myDataView;
   }

   DataSet^ GetDataSetFromDataSource()
   {
      // Create a DataSet variable, and set it to the DataSource.
      DataSet^ myDataSet;
      myDataSet = (DataSet^)(dataGrid1->DataSource);
      return myDataSet;
   }
private void BindToDataView(DataGrid myGrid){
    // Create a DataView using the DataTable.
    DataTable myTable = new DataTable("Suppliers");
    // Insert code to create and populate columns.
    DataView myDataView = new DataView(myTable);
    myGrid.DataSource = myDataView;
 }
 private void BindToDataSet(DataGrid myGrid){
    // Create a DataSet.
    DataSet myDataSet = new DataSet("myDataSet");
    // Insert code to populate DataSet with several tables.
    myGrid.DataSource = myDataSet;
    // Use the DataMember property to specify the DataTable.
    myGrid.DataMember = "Suppliers";
 }
 private DataView GetDataViewFromDataSource(){
    // Create a DataTable variable, and set it to the DataSource.
    DataView myDataView;
    myDataView = (DataView) dataGrid1.DataSource;
    return myDataView;
 }
 private DataSet GetDataSetFromDataSource(){
    // Create a DataSet variable, and set it to the DataSource.
    DataSet myDataSet;
    myDataSet = (DataSet) dataGrid1.DataSource;
    return myDataSet;
 }
Private Sub BindToDataView(myGrid As DataGrid)
    ' Create a DataView using the DataTable.
    Dim myTable As New DataTable("Suppliers")
    ' Insert code to create and populate columns.
    Dim myDatatView As New DataView(myTable)
    myGrid.DataSource = myDatatView
End Sub

Private Sub BindToDataSet(myGrid As DataGrid)
    ' Create a DataSet.
    Dim myDataSet As New DataSet("myDataSet")
    ' Insert code to populate DataSet with several tables.
    myGrid.DataSource = myDataSet
    ' Use the DataMember property to specify the DataTable.
    myGrid.DataMember = "Suppliers"
End Sub

Private Function GetDataViewFromDataSource() As DataView
    ' Create a DataTable variable, and set it to the DataSource.
    Dim myDatatView As DataView
    myDatatView = CType(dataGrid1.DataSource, DataView)
    Return myDatatView
End Function 'GetDataViewFromDataSource

Private Function GetDataSetFromDataSource() As DataSet
    ' Create a DataSet variable, and set it to the DataSource.
    Dim myDataSet As DataSet
    myDataSet = CType(dataGrid1.DataSource, DataSet)
    Return myDataSet
End Function 'GetDataSetFromDataSource

注釈

実行時に、メソッドをSetDataBinding使用してプロパティをDataSourceDataMember設定します。

次のデータ ソースが有効です。

データ ソースの Binding 詳細については、クラスの概要を参照してください。

参照に複数の DataSource テーブルが含まれている場合は、バインド先のテーブルを DataMember 指定する文字列をプロパティに設定する必要があります。 たとえば、a または DataViewManager 3 つのテーブルが含まれている場合DataSourceDataSetOrderDetails``Ordersバインド先のテーブルCustomersを指定する必要があります。

DataSourceインターフェイスまたはIListSourceインターフェイスを実装していないオブジェクトにIList設定すると、グリッドで例外がスローされます。

ユーザーがデータを編集できるグリッドを作成できますが、データ ソースとして a を使用しDataView、プロパティを にfalse設定することで、ユーザーが新しい行をAddNew追加できないようにすることができます。

オブジェクトの DataGrid 厳密に型指定された配列にバインドするには、オブジェクト型にパブリック プロパティを含める必要があります。 配列を表示するオブジェクトをDataGridTableStyle作成するには、プロパティをDataGridTableStyle.MappingNametypenameオブジェクト型の名前に置き換える場所typenameに設定します。 また、プロパティでは MappingName 大文字と小文字が区別されることに注意してください。型名は正確に一致する必要があります。 例については、 MappingName プロパティを参照してください。

にバインドDataGridArrayListすることもできます。 特徴は、複数の ArrayList 型のオブジェクトを含めることができるが、リスト内のすべての項目が DataGrid 最初の項目と同じ型の場合にのみ、そのようなリストにバインドできることです。 つまり、すべてのオブジェクトが同じ型であるか、リストの最初の項目と同じクラスから継承する必要があります。 たとえば、リスト内の最初の項目が a Controlの場合、2 番目の項目は ( TextBox 継承 Control元) になります。 一方、最初の項目が a TextBoxの場合は、2 番目のオブジェクトを a Controlにすることはできません。 さらに、 ArrayList バインドされている場合は、項目が含まれている必要があります。 空 ArrayList の場合、グリッドは空になります。 また、オブジェクトにはパブリック プロパティが ArrayList 含まれている必要があります。 にArrayListバインドする場合は、"ArrayList" (型名) に設定MappingNameDataGridTableStyleします。

適用対象

こちらもご覧ください