DataGridView.DataMember 属性

定义

获取或设置 DataGridView 正在为其显示数据的数据源中的列表或表的名称。

C#
public string DataMember { get; set; }

属性值

DataSourceDataGridView 显示其数据的表或列表的名称。 默认值为 Empty

例外

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

示例

下面的代码示例演示了此属性的常见用法。 在示例中,名为 的DataGridView控件绑定到名为 的 DataSetcustomersDataSetcustomersDataGridViewDataSet 包含一个名为“Customers”的表。 属性 DataMember 设置为表的名称。

C#
private void BindData()
{
    customersDataGridView.AutoGenerateColumns = true;
    customersDataGridView.DataSource = customersDataSet;
    customersDataGridView.DataMember = "Customers";
}

注解

当绑定到包含多个列表或表的数据源时,此属性非常有用。 绑定到包含单个列表或表的数据源时,无需设置此属性。 例如,可以将控件DataSet绑定到DataGridView包含单个表的 ,而无需设置此属性。 但是, DataSet 如果 包含多个表,则必须将此属性设置为其中一个表的名称。

适用于

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

另请参阅