英語で読む

次の方法で共有


DataGridView.DataSource プロパティ

定義

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

public object DataSource { get; set; }
public object? DataSource { get; set; }

プロパティ値

表示される DataGridView のデータを格納しているオブジェクト。

例外

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

次のコード例では、単純なデータ バインド DataGridViewを初期化する方法を示します。 また、 プロパティを設定 DataSource する方法も示します。

using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Drawing;

public class Form1 : System.Windows.Forms.Form
{
    private DataGridView dataGridView1 = new DataGridView();
    private BindingSource bindingSource1 = new BindingSource();

    public Form1()
    {
        dataGridView1.Dock = DockStyle.Fill;
        this.Controls.Add(dataGridView1);
        InitializeDataGridView();
    }

    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;
    }

    [STAThreadAttribute()]
    public static void Main()
    {
        Application.Run(new Form1());
    }
}

注釈

クラスはDataGridView、標準のWindows フォームデータ バインディング モデルをサポートしています。 つまり、データ ソースには、次のいずれかのインターフェイスを実装する任意の型を指定できます。

具体的な例については、このセクションの最後にある「例」セクションとタスク テーブルを参照してください。

通常、BindingSource コンポーネントにバインドし、BindingSource コンポーネントを別のデータ ソースにバインドするか、ビジネス オブジェクトを設定します。 BindingSource コンポーネントは、さまざまなデータ ソースにバインドすることができ、多くのデータ バインディングの問題を自動的に解決できるため、推奨されるデータ ソースです。

複数のリストまたはテーブルを含むデータ ソースにバインドする場合は、 プロパティを DataMember 、バインド先のリストまたはテーブルを指定する文字列に設定する必要があります。 ただし、複数のBindingSourceリストまたはテーブルを含むコンポーネントにバインドする場合は、代わりに コンポーネントの プロパティをBindingSource設定DataMemberできます。

データベース データではなくオブジェクト コレクションにバインドする場合、通常、 プロパティによってDefaultCellStyle返されるオブジェクトの プロパティは、 の既定値DBNull.Valueを使用するのではなく にnull設定DataSourceNullValueします。これはデータベース データに適しています。

詳細については、「Windows フォーム DataGridView コントロールでのデータの表示」を参照してください。 次の表に、 プロパティに関連する一般的なタスクへの直接リンクを DataSource 示します。

チュートリアル: 2 つのWindows フォーム DataGridView コントロールを使用してマスター/詳細フォームを作成する」および「How to: Bind Objects to Windows フォーム DataGridView Controls」を参照してください。

適用対象

製品 バージョン
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

こちらもご覧ください