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

Dim instance As DataGridView Dim value As Object value = instance.DataSource instance.DataSource = value
/** @property */ public Object get_DataSource () /** @property */ public void set_DataSource (Object value)
表示される DataGridView のデータを格納しているオブジェクト。


DataGridView クラスは、標準の Windows フォーム データ バインディング モデルをサポートします。つまり、データ ソースには、次のいずれかのインターフェイスを実装する任意の型を使用できます。
通常、BindingSource コンポーネントにバインドし、BindingSource コンポーネントを別のデータ ソースにバインドするか、このコンポーネントにビジネス オブジェクトを設定します。データ ソースには BindingSource コンポーネントを使用することをお勧めします。このコンポーネントを使用することで、さまざまなデータ ソースにバインドできるほか、データ バインディングに関する各種の問題を自動的に解決できます。
複数のリストまたはテーブルを含むデータ ソースにバインドする場合、DataMember プロパティを、バインド先のリストまたはテーブルを指定する文字列に設定する必要があります。ただし、複数のリストまたはテーブルを含む BindingSource コンポーネントにバインドする場合は、その代わりに、BindingSource コンポーネントの DataMember プロパティを設定できます。
データベース データではなくオブジェクト コレクションにバインドする場合、通常は、DefaultCellStyle プロパティによって返されるオブジェクトの DataSourceNullValue プロパティを null 参照 (Visual Basic では Nothing) に設定します。System.DbNull.Value の既定値 (データベース データの場合に適している) は使用しません。
詳細については、「DataGridView コントロールの概要 (Windows フォーム)」を参照してください。

単純なデータ バインド DataGridView を初期化する方法を次のコード例に示します。この例では、DataSource プロパティの設定方法も示します。この例を実行するには、dataGridView1 という名前の DataGridView を含むフォームに次のコードを貼り付け、コードで指定された connectionString 変数の値を、例が実行されるシステムに対して有効な文字列に置換して、フォームのコンストラクタまたは Load イベント ハンドラから InitializeDataGridView メソッドを呼び出します。
Private Sub InitializeDataGridView() Try ' Set up the DataGridView. With Me.dataGridView1 ' Automatically generate the DataGridView columns. .AutoGenerateColumns = True ' Set up the data source. bindingSource1.DataSource = GetData("Select * From Products") .DataSource = bindingSource1 ' Automatically resize the visible rows. .AutoSizeRowsMode = _ DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders ' Set the DataGridView control's border. .BorderStyle = BorderStyle.Fixed3D ' Put the cells in edit mode when user enters them. .EditMode = DataGridViewEditMode.EditOnEnter End With Catch ex As SqlException MessageBox.Show("To run this sample replace " _ & "connection.ConnectionString with a valid connection string" _ & " to a Northwind database accessible to your system.", _ "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) System.Threading.Thread.CurrentThread.Abort() End Try End Sub Private Shared Function GetData(ByVal sqlCommand As String) _ As DataTable Dim connectionString As String = _ "Integrated Security=SSPI;Persist Security Info=False;" _ & "Initial Catalog=Northwind;Data Source=localhost" Dim northwindConnection As SqlConnection = _ New SqlConnection(connectionString) Dim command As New SqlCommand(sqlCommand, northwindConnection) Dim adapter As SqlDataAdapter = New SqlDataAdapter() adapter.SelectCommand = command Dim table As New DataTable table.Locale = System.Globalization.CultureInfo.InvariantCulture adapter.Fill(table) Return table End Function
private void InitializeDataGridView() { try { // Set up the DataGridView. dataGridView1.Dock = DockStyle.Fill; // Automatically generate the DataGridView columns. dataGridView1.AutoGenerateColumns = true; // Set up the data source. bindingSource1.DataSource = GetData("Select * From Products"); dataGridView1.DataSource = bindingSource1; // Automatically resize the visible rows. dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders; // Set the DataGridView control's border. dataGridView1.BorderStyle = BorderStyle.Fixed3D; // Put the cells in edit mode when user enters them. dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter; } catch (SqlException) { MessageBox.Show("To run this sample replace connection.ConnectionString" + " with a valid connection string to a Northwind" + " database accessible to your system.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); System.Threading.Thread.CurrentThread.Abort(); } } private static DataTable GetData(string sqlCommand) { string connectionString = "Integrated Security=SSPI;" + "Persist Security Info=False;" + "Initial Catalog=Northwind;Data Source=localhost"; SqlConnection northwindConnection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(sqlCommand, northwindConnection); SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = command; DataTable table = new DataTable(); table.Locale = System.Globalization.CultureInfo.InvariantCulture; adapter.Fill(table); return table; }
void InitializeDataGridView() { try { // Set up the DataGridView. dataGridView1->Dock = DockStyle::Fill; // Automatically generate the DataGridView columns. dataGridView1->AutoGenerateColumns = true; // Set up the data source. bindingSource1->DataSource = GetData( "Select * From Products" ); dataGridView1->DataSource = bindingSource1; // Automatically resize the visible rows. dataGridView1->AutoSizeRowsMode = DataGridViewAutoSizeRowsMode::DisplayedCellsExceptHeaders; // Set the DataGridView control's border. dataGridView1->BorderStyle = BorderStyle::Fixed3D; // Put the cells in edit mode when user enters them. dataGridView1->EditMode = DataGridViewEditMode::EditOnEnter; } catch ( SqlException^ ) { MessageBox::Show( "To run this sample replace connection.ConnectionString" " with a valid connection string to a Northwind" " database accessible to your system.", "ERROR", MessageBoxButtons::OK, MessageBoxIcon::Exclamation ); System::Threading::Thread::CurrentThread->Abort(); } catch ( System::Exception^ ex ) { MessageBox::Show( ex->ToString() ); } } DataTable^ GetData( String^ sqlCommand ) { String^ connectionString = "Integrated Security=SSPI;Persist Security Info=False;" "Initial Catalog=Northwind;Data Source= localhost"; SqlConnection^ northwindConnection = gcnew SqlConnection( connectionString ); SqlCommand^ command = gcnew SqlCommand( sqlCommand,northwindConnection ); SqlDataAdapter^ adapter = gcnew SqlDataAdapter; adapter->SelectCommand = command; DataTable^ table = gcnew DataTable; adapter->Fill( table ); return table; }

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


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

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