DataGridView.DataSource 属性

定义

获取或设置 DataGridView 所显示数据的数据源。

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

属性值

包括要显示的 DataGridView 的数据的对象。

例外

数据源中出现错误,这是由于没有 DataError 事件的处理程序或处理程序已将 ThrowException 属性设置为 true。 通常情况下,可将该异常对象强制转换为类型 FormatException

示例

下面的代码示例演示如何初始化简单的数据绑定 DataGridView。 它还演示如何设置 DataSource 属性。

C#
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 包含多个列表或表的组件时,可以改为设置 DataMember 组件的 BindingSource 属性。

绑定到对象集合而不是数据库数据时,通常会将 属性返回DefaultCellStyle的 对象的 属性DBNull.Value设置为 DataSourceNullValuenull ,而不是使用适用于数据库数据的默认值 。

有关详细信息,请参阅在 Windows 窗体 DataGridView 控件中显示数据。 下表提供了指向与 属性相关的 DataSource 常见任务的直接链接。

请参阅演练:使用两个Windows 窗体 DataGridView 控件创建主窗体/详细信息窗体如何:将对象绑定到Windows 窗体 DataGridView 控件

适用于

产品 版本
.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

另请参阅