DataGridViewComboBoxCell.ValueMember Propriété

Définition

Obtient ou définit une chaîne qui spécifie où rassembler les valeurs sous-jacentes utilisées dans la liste déroulante.Gets or sets a string that specifies where to gather the underlying values used in the drop-down list.

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

Valeur de propriété

Chaîne spécifiant le nom d'une propriété ou d'une colonne.A string specifying the name of a property or column. La valeur par défaut est Empty, ce qui indique que cette propriété est ignorée.The default value is Empty, which indicates that this property is ignored.

Exceptions

La propriété DataSource n'est pas null et la valeur spécifiée lors de la définition de cette propriété n'est pas null ou Empty et ne nomme pas une propriété ou colonne valide dans la source de données.The DataSource property is not null and the specified value when setting this property is not null or Empty and does not name a valid property or column in the data source.

Exemples

L’exemple de code suivant illustre l’utilisation de la propriété DataGridViewComboBoxColumn.ValueMember, qui est similaire à cette propriété.The following code example demonstrates the use of the DataGridViewComboBoxColumn.ValueMember property, which is similar to this property. Cet exemple fait partie d’un exemple plus complet disponible dans la rubrique vue d’ensemble de la classe 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

Remarques

ValueMember représente la valeur correspondante d’une sélection.ValueMember represents the corresponding value of a selection. En revanche, la propriété DisplayMember représente les informations de texte affichées dans la liste déroulante.In contrast, the DisplayMember property represents the text information displayed in the drop-down list.

La propriété DataSource spécifie la source de données pour la valeur des sélections affichées dans la liste déroulante.The DataSource property specifies the data source for the value of the selections displayed in the drop-down list.

Si les données des sélections affichées par le DataGridViewComboBoxCell sont supposées être dessinées à partir d’une propriété ou d’une colonne non définie par défaut du DataSource, ValueMember devez être défini en plus de DataSource.If the data for the selections displayed by the DataGridViewComboBoxCell is supposed to be drawn from a nondefault property or column of the DataSource, then ValueMember must be set in addition to DataSource.

Lorsque DataSource a la valeur d’un tableau de chaînes, ValueMember n’a pas besoin d’être défini, car chaque chaîne du tableau sera utilisée comme une chaîne d’affichage valide et une valeur sous-jacente valide.When DataSource is set to a string array, ValueMember does not need to be set because each string in the array will be used as a valid display string and a valid underlying value.

Une autre façon de charger des sélections de zone de liste modifiable consiste à utiliser la propriété Items.Another way of loading combo box selections is to use the Items property. ValueMember doit contenir le nom de la propriété à partir duquel rassembler les sélections.ValueMember must contain the property name from which to gather the selections.

S’applique à

Voir aussi