この記事は機械翻訳されています。英語版の記事を表示するには、[英語] のチェック ボックスをオンにしてください。また、テキストにマウス ポインターを合わせると、ポップアップ ウィンドウに英語のテキストを表示することもできます。
翻訳
英語

DataGridView.DataSource プロパティ

 

公開日: 2016年10月

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

名前空間:   System.Windows.Forms
アセンブリ:  System.Windows.Forms (System.Windows.Forms.dll 内)

public object DataSource { get; set; }

プロパティ値

Type: System.Object

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

Exception Condition
Exception

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

The T:System.Windows.Forms.DataGridView class supports the standard Windows Forms data-binding model. This means the data source can be of any type that implements one of the following interfaces:

  • The T:System.Collections.IList interface, including one-dimensional arrays.

  • The T:System.ComponentModel.IListSource interface, such as the T:System.Data.DataTable and T:System.Data.DataSet classes.

  • The T:System.ComponentModel.IBindingList interface, such as the T:System.ComponentModel.BindingList`1 class.

  • The T:System.ComponentModel.IBindingListView interface, such as the T:System.Windows.Forms.BindingSource class.

For specific examples, see the Example section and the task table at the end of this section.

Typically, you will bind to a T:System.Windows.Forms.BindingSource component and bind the T:System.Windows.Forms.BindingSource component to another data source or populate it with business objects. The T:System.Windows.Forms.BindingSource component is the preferred data source because it can bind to a wide variety of data sources and can resolve many data binding issues automatically.

When binding to a data source that contains multiple lists or tables, you must set the P:System.Windows.Forms.DataGridView.DataMember property to a string that specifies the list or table to bind to. When binding to a T:System.Windows.Forms.BindingSource component that contains multiple lists or tables, however, you can set the P:System.Windows.Forms.BindingSource.DataMember property of the T:System.Windows.Forms.BindingSource component instead.

When binding to an object collection rather than to database data, you will typically set the P:System.Windows.Forms.DataGridViewCellStyle.DataSourceNullValue property of the object returned by the P:System.Windows.Forms.DataGridView.DefaultCellStyle property to null rather than using the default value of F:System.DBNull.Value, which is appropriate for database data.

For more information, see Displaying Data in the Windows Forms DataGridView Control. The following table provides direct links to common tasks related to the P:System.Windows.Forms.DataGridView.DataSource property.

See Walkthrough: Creating a Master/Detail Form Using Two Windows Forms DataGridView Controlsand How to: Bind Objects to Windows Forms DataGridView Controls.

The following code example demonstrates how to initialize a simple data-bound T:System.Windows.Forms.DataGridView. It also demonstrates how to set the P:System.Windows.Forms.DataGridView.DataSource property.

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

.NET Framework
2.0 以降で使用可能
トップに戻る
表示: