Click here to Skip to main content
14,875,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I've searched links that have some instructions to create a custom checked combobox. Then, I host it into a DataGridViewComboBoxColumn.

I created the custom checked combobox in design time. Added it to the datagridview on form load. Upon clicking the cell, I use CellBeginEdit() to set the custom control into the cell. Then, populate data from database.

Private Sub dataGridView1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs)
  If e.ColumnIndex = 1 Then
    'set location and size of checkedlistbox1.  
    Dim rec As Rectangle = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)

    customCheckedCombo1.Location = rec.Location
    customCheckedCombo1.Visible = True
    Call FillColorList()

  End If
End Sub

Everything works fine on first row. But, when I click the custom checked combobox on the second row, the checked values from first row jumps to 2nd row, and vice-versa if I click the first row again. Adding more rows, I see I can only have 1 check value jumping according which row is clicked.

I cannot figure out why. Appreciate if someone can help. Thanks in advance.

What I have tried:

DataGridViewComboBoxColumn, DataGridViewComboBoxCell
Updated 11-Mar-16 9:54am

1 solution

Hi there member 8599340,

To fix this, you need to create two classes which together will make it possible to use your custom Combobox in a DataGridView. The first class should derive the DataGridViewComboBoxCell class, and the other should derive the DataGridViewComboBoxColumn class.

Suppose your custom ComboBox is implemented in a class named MyCBO, these two classes might look like this:
' This is the class that represents your cell which can use your ComboBox class
Public Class MyDataGridViewComboBoxCell
    Inherits DataGridViewComboBoxCell

    Public Sub New()
    End Sub

    ' You must override the EditType property to return the cell's 
    ' editing control type, which is your custom ComboBox class...
    Public Overrides ReadOnly Property EditType() As Type
            Return GetType(MyCBO)
        End Get
    End Property

    ' You must also override this method to initialize the ComboBox instance...
    ' This method will be called each time a cell in the column enters edit-mode, 
    ' so you can fill the ComboBox instance based on the value of the edited cell
    Public Overrides Sub InitializeEditingControl(_
        ByVal rowIndex As Integer, _
        ByVal formattedValue As Object, _
        ByVal cellStyle As DataGridViewCellStyle)

        ' Call base...
        MyBase.InitializeEditingControl(rowIndex, formattedValue, cellStyle)

        ' Convert the cell's EditingControl to your custom ComboBox type...
        Dim ctl As MyCBO = CType(DataGridView.EditingControl, MyCBO)

        ' Make sure you have an instance...
        If ctl IsNot Nothing Then
            ' Populate the ComboBox, passing the instance as a parameter

            ' Set the value of the editing control instance to the current cell value.
            ctl.SelectedValue = formattedValue
        End If
    End Sub
End Class

' This is the class that represents your column which can use your cell class
Public Class MyDataGridViewComboBoxColumn
    Inherits DataGridViewComboBoxColumn

    Public Sub New()

        ' Specify the column to use your custom cell class...
        MyBase.CellTemplate = New MyDataGridViewComboBoxCell()
    End Sub

End Class

And that's really all there is to it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900