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: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:
So in your case, you will need to do something similar to the following:
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 thedatagridviewcomboboxcolumn
.