Very good afternoon to all, The problem I have now is that I can not get the value selected as a combobox, I'm trying to set the text and value to each item in the combobox of each cell in the datagrid. My Code:
CLASS MyListItem:
Public Class MyListItem
Private mText As String
Private mValue As String
Public Sub New(ByVal pText As String, ByVal pValue As String)
mText = pText
mValue = pValue
End Sub
Public ReadOnly Property Text() As String
Get
Return mText
End Get
End Property
Public ReadOnly Property Value() As String
Get
Return mValue
End Get
End Property
Public Overrides Function ToString() As String
Return mText
End Function
End Class
Form Load:
DataGridView1.Rows.Add()
Dim dgvcbc As DataGridViewComboBoxCell = DirectCast(DataGridView1.Rows(0).Cells(0), DataGridViewComboBoxCell)
dgvcbc.Items.Add(New MyListItem("Text to be displayed", "value of the item"))
Try to Display Selected value:
Dim oItem As MyListItem = CType(**dgvcbc.SelectedItem**, MyListItem)
MessageBox.Show("The Value of the Item selected is: " & oItem.Value)
ERROR: 'SelectedItem' is not a member of 'System.Windows.Forms.DataGridViewComboBoxCell'
If anyone has any idea how to set the values and text to each item of each cell with combobox, I'd be very grateful thanks
You need to use the Value
property according to the MSDN documentation:
Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.
To load the DataGridViewComboBoxCell you need to set the DataSource.
Depending on the type of data in the datasource, you may also need to set the DisplayMember to choose the property or column name to show in the display portion of the control and the ValueMember to choose the property or column name that is used to set the Value property of the control when an item is selected.
Here is some additional guidance from MSDN on the datasource:
Typically this property will be set for an entire column of cells through the DataGridViewComboBoxColumn.DataSource property.
If possible, set DataSource to a source containing only the possible selections, like a column of selections. Then the DisplayMember property doesn't need to be set. But if the source is more complicated, set DisplayMember to the name of the property or column from which to retrieve possible selections.
If DataSource is set to a string array, then ValueMember and DisplayMember do not need to be set because each string in the array will be used for both value and display.
So in your case, you will need to do something similar to the following:
Dim cListItems As New System.Collections.Generic.List(Of MyListItem)
cListItems.Add(New MyListItem("Text to be displayed", "value of the item"))
Dim dgvcbc As DataGridViewComboBoxCell = DirectCast(DataGridView1.Rows(0).Cells(0), DataGridViewComboBoxCell)
dgvcbc.DataSource = cListItems
dgvcbc.DisplayMember = "Text"
dgvcbc.ValueMember = "Value"
Finally, if the values are the same for all cells, then you will probably want to assign the datasource to the column when you create it. All of the above code will remain the same, except you will replace the dgvcbc
reference with the variable that holds the datagridviewcomboboxcolumn
.