DataGridViewComboBoxColumn.DisplayMember DataGridViewComboBoxColumn.DisplayMember DataGridViewComboBoxColumn.DisplayMember DataGridViewComboBoxColumn.DisplayMember Property

定义

获取或设置一个字符串,此字符串指定要从其中检索在组合框中显示的字符串的属性或列。Gets or sets a string that specifies the property or column from which to retrieve strings for display in the combo boxes.

public:
 property System::String ^ DisplayMember { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.ComponentModel.TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string DisplayMember { get; set; }
member this.DisplayMember : string with get, set
Public Property DisplayMember As String

属性值

String,它指定在 DataSource 属性中所指定数据源中的属性或列的名称。A String that specifies the name of a property or column in the data source specified in the DataSource property. 默认值为 EmptyThe default is Empty.

异常

示例

下面的代码示例演示如何使用DataGridViewComboBoxColumn来帮助进行数据输入的标题列。The following code example demonstrates how to use a DataGridViewComboBoxColumn to aid in data entry of the title column. DisplayMember 设置为负责包含用户可查看文本的属性名称。DisplayMember is set to the property name responsible for containing the user-viewable text. 在此示例中,DisplayMember设置为相同的值ValueMember因为没有映射是必需的。In this example, the DisplayMember is set to the same value as ValueMember because no mapping is necessary. 此示例摘自一个更大的示例中提供DataGridViewComboBoxColumn类概述主题。This example is part of a larger example available in the DataGridViewComboBoxColumn class overview topic.

private:
    DataGridViewComboBoxColumn^ CreateComboBoxColumn()
    {
        DataGridViewComboBoxColumn^ column =
            gcnew DataGridViewComboBoxColumn();
        {
            column->DataPropertyName = ColumnName::TitleOfCourtesy.ToString();
            column->HeaderText = ColumnName::TitleOfCourtesy.ToString();
            column->DropDownWidth = 160;
            column->Width = 90;
            column->MaxDropDownItems = 3;
            column->FlatStyle = FlatStyle::Flat;
        }
        return column;
    }

private:
    void SetAlternateChoicesUsingDataSource(DataGridViewComboBoxColumn^ comboboxColumn)
    {
        {
            comboboxColumn->DataSource = RetrieveAlternativeTitles();
            comboboxColumn->ValueMember = ColumnName::TitleOfCourtesy.ToString();
            comboboxColumn->DisplayMember = comboboxColumn->ValueMember;
        }
    }

private:
    DataTable^ RetrieveAlternativeTitles()
    {
        return Populate("SELECT distinct TitleOfCourtesy FROM Employees");
    }

    String^ connectionString;

private:
    DataTable^ Populate(String^ sqlCommand)
    {
        SqlConnection^ northwindConnection = gcnew SqlConnection(connectionString);
        northwindConnection->Open();

        SqlCommand^ command = gcnew SqlCommand(sqlCommand, northwindConnection);
        SqlDataAdapter^ adapter = gcnew SqlDataAdapter();
        adapter->SelectCommand = command;

        DataTable^ table = gcnew DataTable();
        adapter->Fill(table);

        return table;
    }

	// Using an enum provides some abstraction between column index
    // and column name along with compile time checking, and gives
    // a handy place to store the column names.
    enum class ColumnName
    {
        EmployeeID,
        LastName,
        FirstName,
        Title,
        TitleOfCourtesy,
        BirthDate,
        HireDate,
        Address,
        City,
        Region,
        PostalCode,
        Country,
        HomePhone,
        Extension,
        Photo,
        Notes,
        ReportsTo,
        PhotoPath,
        OutOfOffice
    };
private DataGridViewComboBoxColumn CreateComboBoxColumn()
{
    DataGridViewComboBoxColumn column =
        new DataGridViewComboBoxColumn();
    {
        column.DataPropertyName = ColumnName.TitleOfCourtesy.ToString();
        column.HeaderText = ColumnName.TitleOfCourtesy.ToString();
        column.DropDownWidth = 160;
        column.Width = 90;
        column.MaxDropDownItems = 3;
        column.FlatStyle = FlatStyle.Flat;
    }
    return column;
}

private void SetAlternateChoicesUsingDataSource(DataGridViewComboBoxColumn comboboxColumn)
{
    {
        comboboxColumn.DataSource = RetrieveAlternativeTitles();
        comboboxColumn.ValueMember = ColumnName.TitleOfCourtesy.ToString();
        comboboxColumn.DisplayMember = comboboxColumn.ValueMember;
    }
}

private DataTable RetrieveAlternativeTitles()
{
    return Populate("SELECT distinct TitleOfCourtesy FROM Employees");
}

string connectionString =
    "Integrated Security=SSPI;Persist Security Info=False;" +
    "Initial Catalog=Northwind;Data Source=localhost";

private DataTable Populate(string sqlCommand)
{
    SqlConnection northwindConnection = new SqlConnection(connectionString);
    northwindConnection.Open();

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

// Using an enum provides some abstraction between column index
// and column name along with compile time checking, and gives
// a handy place to store the column names.
enum ColumnName
{
    EmployeeId,
    LastName,
    FirstName,
    Title,
    TitleOfCourtesy,
    BirthDate,
    HireDate,
    Address,
    City,
    Region,
    PostalCode,
    Country,
    HomePhone,
    Extension,
    Photo,
    Notes,
    ReportsTo,
    PhotoPath,
    OutOfOffice
};
Private Function CreateComboBoxColumn() _
    As DataGridViewComboBoxColumn
    Dim column As New DataGridViewComboBoxColumn()

    With column
        .DataPropertyName = ColumnName.TitleOfCourtesy.ToString()
        .HeaderText = ColumnName.TitleOfCourtesy.ToString()
        .DropDownWidth = 160
        .Width = 90
        .MaxDropDownItems = 3
        .FlatStyle = FlatStyle.Flat
    End With
    Return column
End Function

Private Sub SetAlternateChoicesUsingDataSource( _
    ByVal comboboxColumn As DataGridViewComboBoxColumn)
    With comboboxColumn
        .DataSource = RetrieveAlternativeTitles()
        .ValueMember = ColumnName.TitleOfCourtesy.ToString()
        .DisplayMember = .ValueMember
    End With
End Sub

Private Function RetrieveAlternativeTitles() As DataTable
    Return Populate( _
        "SELECT distinct TitleOfCourtesy FROM Employees")
End Function

Private connectionString As String = _
        "Integrated Security=SSPI;Persist Security Info=False;" _
        & "Initial Catalog=Northwind;Data Source=localhost"

Private Function Populate(ByVal sqlCommand As String) As DataTable
    Dim northwindConnection As New SqlConnection(connectionString)
    northwindConnection.Open()

    Dim command As New SqlCommand(sqlCommand, _
        northwindConnection)
    Dim adapter As New SqlDataAdapter()
    adapter.SelectCommand = command
    Dim table As New DataTable()
    table.Locale = System.Globalization.CultureInfo.InvariantCulture
    adapter.Fill(table)

    Return table
End Function

' Using an enum provides some abstraction between column index
' and column name along with compile time checking, and gives
' a handy place to store the column names.
Enum ColumnName
    EmployeeId
    LastName
    FirstName
    Title
    TitleOfCourtesy
    BirthDate
    HireDate
    Address
    City
    Region
    PostalCode
    Country
    HomePhone
    Extension
    Photo
    Notes
    ReportsTo
    PhotoPath
    OutOfOffice
End Enum

注解

中的列存储的值的数据DataGridViewComboBoxCell对象均来自DataGridView.DataSourceThe data for the values stored in a column of DataGridViewComboBoxCell objects is obtained from the DataGridView.DataSource. 如果此数据来自非默认属性或列,则DisplayMember属性必须设置为必需的属性名称或列名称。If this data comes from a nondefault property or column, then the DisplayMember property must be set to the necessary property name or column name.

如果单元格的值是对用户不可见的内部值,然后使用DisplayMemberValueMember将映射到用户可查看值的内部单元格的值。If the cell values are internal values not visible to the user, then use DisplayMember and ValueMember to map the internal cell values to user-viewable values.

DataSource属性设置为一个字符串数组,DisplayMember属性不需要设置,因为将使用数组中的每个字符串,作为有效的显示字符串和有效的基础值。When the DataSource property is set to a string array, the DisplayMember property does not need to be set because each string in the array will be used as a valid display string and as a valid underlying value.

获取或设置此属性获取或设置DisplayMember属性返回的对象CellTemplate属性。Getting or setting this property gets or sets the DisplayMember property of the object returned by the CellTemplate property. 设置此属性还将设置DisplayMember列显示属性中的列和刷新每个单元格。Setting this property also sets the DisplayMember property of every cell in the column and refreshes the column display. 若要重写每个单元格的指定的值,设置单元格的值后设置列的值。To override the specified value for individual cells, set the cell values after you set the column value.

适用于

另请参阅