DataGridView.DataMember 属性


获取或设置 DataGridView 正在为其显示数据的数据源中的列表或表的名称。Gets or sets the name of the list or table in the data source for which the DataGridView is displaying data.

 property System::String ^ DataMember { System::String ^ get(); void set(System::String ^ value); };
public string DataMember { get; set; }
member this.DataMember : string with get, set
Public Property DataMember As String


DataSourceDataGridView 显示其数据的表或列表的名称。The name of the table or list in the DataSource for which the DataGridView is displaying data. 默认值为 EmptyThe default is Empty.


数据源中出现错误,这是由于没有 DataError 事件的处理程序或处理程序已将 ThrowException 属性设置为 trueAn error occurred in the data source and either there is no handler for the DataError event or the handler has set the ThrowException property to true. 通常情况下,可将该异常对象强制转换为类型 FormatExceptionThe exception object can typically be cast to type FormatException.


下面的代码示例说明了此属性的常见用法。The following code example illustrates a common use for this property. 在此示例中,名为 customersDataGridView DataGridView 控件绑定到名为 customersDataSetDataSetIn the example, a DataGridView control named customersDataGridView is bound to a DataSet named customersDataSet. DataSet 包含一个名为 "Customers" 的表。This DataSet contains a table named "Customers". DataMember 属性设置为表的名称。The DataMember property is set to the name of the table.

private void BindData()
    customersDataGridView.AutoGenerateColumns = true;
    customersDataGridView.DataSource = customersDataSet;
    customersDataGridView.DataMember = "Customers";
Private Sub BindData()

    With customersDataGridView
        .AutoGenerateColumns = True
        .DataSource = customersDataSet
        .DataMember = "Customers"
    End With

End Sub


绑定到包含多个列表或表的数据源时,此属性很有用。This property is useful when binding to a data source that contains multiple lists or tables. 绑定到包含单个列表或表的数据源时,无需设置此属性。You do not need to set this property when binding to a data source that contains a single list or table. 例如,可以将 DataGridView 控件绑定到包含单个表的 DataSet,而无需设置此属性。For example, you can bind a DataGridView control to a DataSet that contains a single table without setting this property. 但是,如果 DataSet 包含多个表,则必须将此属性设置为其中一个表的名称。If the DataSet contains multiple tables, however, you must set this property to the name of one of the tables.