DataGrid.DataSource プロパティ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As DataGrid Dim value As Object value = instance.DataSource instance.DataSource = value
/** @property */ public Object get_DataSource () /** @property */ public void set_DataSource (Object value)
データ ソースとして機能するオブジェクト。

実行時に DataSource プロパティと DataMember プロパティを設定するには、SetDataBinding メソッドを使用します。
-
DataTable
-
DataView
-
DataViewManager
データ ソースの詳細については、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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からDataGrid.DataSource プロパティを検索する場合は、下記のリンクをクリックしてください。

- DataGrid.DataSource プロパティのページへのリンク