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

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

DataGridView.DataSource プロパティ

メモ : このプロパティは、.NET Framework version 2.0新しく追加されたものです。

DataGridView でデータ表示される対象データ ソース取得または設定します

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

Dim instance As DataGridView
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)

プロパティ
表示される DataGridViewデータ格納しているオブジェクト

例外例外
例外種類条件

Exception

データ ソースエラー発生しましたが、DataError イベントハンドラ存在しないか、このハンドラによって DataGridViewDataErrorEventArgs.ThrowException プロパティtrue設定されました。例外オブジェクト通常、FormatException 型にキャストできます

解説解説

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;
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataGridView クラス
DataGridView メンバ
System.Windows.Forms 名前空間
IList
IListSource
DataTable
DataSet
IBindingList
BindingList
IBindingListView
BindingSource クラス
DataGridViewCellStyle
DataGridViewCellStyle.DataSourceNullValue
DefaultCellStyle
DataGridView.DataMember プロパティ
その他の技術情報
DataGridView コントロール (Windows フォーム)



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

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

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

   

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



DataGridView.DataSource プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2024 GRAS Group, Inc.RSS