Search code examples
vb.netdatagridviewdatagridviewcomboboxcell

Why can't I assign this DataGridViewComboBoxCell to the cell I want?


I'm having a weird set of problems with my DataGridViewComboBoxCell objects. FYI, I am using the Cell rather than the Column because my data is organized by row, not by column.

My first problem is that I can't seem to get the assignment right to my DataGridView. It's fine when I do a straight assignment in my code:

Dim MyDropDown As New DataGridViewComboBoxCell()
'Code here to populate dropdown
MyForm.DataGridView1(col, row) = MyDropDown

I run into trouble when I try to use a procedure to do the same thing:

Dim MyDropDown As New DataGridViewComboBoxCell()
'Code here to populate dropdown
SetUpDataViewGridDropdown(MyDropDown, MyForm.DataGridView1(col, row))

. . .

Public Sub SetUpDataViewGridDropdown(ByRef dd As DataGridViewComboBoxCell, _
                                 ByRef ddTarget As DataGridViewCell)

ddTarget = dd
'Do some more interesting stuff here
End Sub

I don't get an error, but the DataGridView renders without displaying the dropdown. So the assignment doesn't seem to be happening. What am I missing?


Solution

  • You're doing it wrong.

    MyForm.DataGridView1(col, row) gets the cell currently in the DataGridView. So what you're doing in your function is just changing the referenced object of ddTarget variable, which is useless.

    Try something like this :

    Public Sub SetUpDataViewGridDropdown(ByRef dd As DataGridViewComboBoxCell, _
                                 ByVal rowIndex As Int, _
                                 ByVal columnIndex As Int)
    
    MyForm.DataGridView1(columnIndex, rowIndex) = dd 
    'Do some more interesting stuff here
    End Sub