Changing to DataGridViewComboBoxCell

pettrer

Well-known member
Joined
Sep 5, 2008
Messages
92
Programming Experience
10+
Hi all,

I'm trying to get a comboboxcell working in a datagridview. I DO NOT want a comboboxcolumn (that's what I have today) as the logic of my application states that only if the cell has a certain value when the form was opened, there should be a combobox instead of a regular textbox.

I've searched the net for a few hours and come up with the tiny piece of code below, but it doesn't work:

VB.NET:
For i As Integer = 0 To dgvM.Rows.Count - nr
  If NullToStr(dgvM.Rows(i).Cells(COL_ANS).Value) = ans Then
    Dim foljerstr As String = NullToStr(dgvM.Rows(i).Cells(COL_FOLJER).Value)
    Dim combocell As DataGridViewComboBoxCell = dgvM.Rows(i).Cells(COL_FOLJER)
    combocell.Items.Add("YES")
    combocell.Items.Add("NO")
    dgvM.Rows(i).Cells(COL_FOLJER).Value = foljerstr
  End If
Next

The "Dim combocell..." line gives the error: "Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.Windows.Forms.DataGridViewComboBoxCell'."

While I can understand the error message in itself and why it appears, this seems to be the manner in which other people have solved this problem. I'm puzzled!

Warm regards,

Pettrer
 
Dim combocell As DataGridViewComboBoxCell = dgvM.Rows(i).Cells(COL_FOLJER)
The error message says that Cells(COL_FOLJER) is a DataGridViewTextBoxCell, not a DataGridViewComboBoxCell.

You haven't changed cell type in your code anywhere, this is how:
VB.NET:
Dim cell As New DataGridViewComboBoxCell
cell.Items.Add("A")
cell.Items.Add("B")
Me.DataGridView1.Item(col, row) = cell
 
Back
Top