DataGrid.DataSource プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > DataGrid.DataSource プロパティの意味・解説 

DataGrid.DataSource プロパティ

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

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Dim instance As DataGrid
Dim value As Object

value = instance.DataSource

instance.DataSource = value
public Object DataSource { get; set;
 }
/** @property */
public Object get_DataSource ()

/** @property */
public void set_DataSource (Object value)

プロパティ値
データ ソースとして機能するオブジェクト。

解説解説

実行時に DataSource プロパティと DataMember プロパティを設定するには、SetDataBinding メソッドを使用します。

有効なデータ ソースを次に示します。

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

DataSource 参照が複数のテーブルを格納している場合は、DataMember プロパティにバインド先のテーブルを指定する文字列を設定する必要があります。たとえば、DataSource が DataSet または DataViewManager であり、Customers、Orders、および OrderDetails という名前の 3 つのテーブルを格納している場合は、バインド先のテーブルを指定する必要があります。

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

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

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

また、DataGrid を ArrayList にバインドできます。ArrayList の特長は、複数の型のオブジェクトを格納できることです。ただし、DataGrid はリスト内のすべての項目の型が 1 番目の項目の型と同じ場合に限り、リストにバインドできます。つまり、すべてのオブジェクトの型が同じであるか、リスト内の最初の項目と同じクラスからすべてのクラスが継承している必要があります。たとえば、リスト内の最初の項目が Control の場合、2 番目の項目は (Control から継承した) TextBox にできます。逆に、1 番目の項目が TextBox の場合、2 番目のオブジェクトが Control になることはできません。さらに、ArrayList は、バインディングするときは、項目を含んでいる必要があります。空の ArrayList の場合、空のグリッドとなります。さらに、ArrayList のオブジェクトにはパブリック プロパティが含まれている必要があります。ArrayList にバインディングするときは、DataGridTableStyle の MappingName を "ArrayList" (型の名前) に設定します。

使用例使用例

DataSource と、必要に応じて DataMember を設定して、System.Windows.Forms.DataGrid を DataView と DataSet の両方にバインドする方法を次のコード例に示します。この例では、System.Windows.Forms.DataGrid からデータ ソースを返す方法も示します。

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 'BindToDataView

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 'BindToDataSet

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
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:
   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.set_DataSource(myDataView);
} //BindToDataView

private void BindToDataSet(DataGrid myGrid)
{
    // Create a DataSet.
    DataSet myDataSet = new DataSet("myDataSet");
    // Insert code to populate DataSet with several tables.
    myGrid.set_DataSource(myDataSet);
    // Use the DataMember property to specify the DataTable.
    myGrid.set_DataMember("Suppliers");
} //BindToDataSet

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

private DataSet GetDataSetFromDataSource()
{
    // Create a DataSet variable, and set it to the DataSource.
    DataSet myDataSet;
    myDataSet = ((DataSet)(dataGrid1.get_DataSource()));
    return myDataSet;
} //GetDataSetFromDataSource
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataGrid クラス
DataGrid メンバ
System.Windows.Forms 名前空間
DataGrid.DataMember プロパティ
DataSet
DataViewManager
DataView



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「DataGrid.DataSource プロパティ」の関連用語

DataGrid.DataSource プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataGrid.DataSource プロパティのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS