DataGridViewComboBoxColumn::ValueMember Property
Gets or sets a string that specifies the property or column from which to get values that correspond to the selections in the drop-down list.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
public: [TypeConverterAttribute("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")] property String^ ValueMember { String^ get(); void set(String^ value); }
Property Value
Type: System::String^A String that specifies the name of a property or column used in the DataSource property. The default is String::Empty.
| Exception | Condition |
|---|---|
| InvalidOperationException | The value of the CellTemplate property is null. |
See DisplayMember for more information about its relationship with ValueMember.
When the DataSource property is set to a string array, the ValueMember 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.
Getting or setting this property gets or sets the ValueMember property of the object returned by the CellTemplate property. Setting this property also sets the ValueMember 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.
The following code example demonstrates how to use a DataGridViewComboBoxColumn to aid in entering data into the TitleOfCourtesy column. 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 };
Available since 2.0