Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've created a custom DataGridComboBoxColumn which works fine. I've filled it's list using a BindingSorce. The DataGridView are also uses another BindingSorce. All works fine except when user focus on combo, it shows the first item and when leave the combo, it shows the actual item. How do I solve this problem ?

Heres my code for examine. Thanks in advance.
VB.NET
Class DVG_ComboBoxControl  
    Inherits ComboBox   
    Implements IDataGridViewEditingControl
    Private dataGridViewControl As DataGridView = Nothing   
    Private valueIsChanged As Boolean = False               
    Private rowIndexNum As Integer = 0                      
    Public Sub New()
        MyBase.New()
        AddHandler Me.SelectedIndexChanged, AddressOf ComboBoxControl_SelectedIndexChanged
    End Sub
    Public Sub ComboBoxControl_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Me.NotifyDataGridViewOfValueChange()
    End Sub
    Protected Overridable Sub NotifyDataGridViewOfValueChange()
        Me.valueIsChanged = True
        If Me.dataGridViewControl IsNot Nothing Then
            Me.dataGridViewControl.NotifyCurrentCellDirty(True)
        End If
    End Sub
    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            SendKeys.Send("{TAB}")
        End If
        MyBase.OnKeyDown(e)
    End Sub
#Region "IDataGridViewEditingControl"
    Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
        Get
            Return Me.Text
        End Get
        Set(ByVal value As Object)
            If TypeOf value Is String Then
                Me.Text = DirectCast(value, String)
            End If
        End Set
    End Property
    Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
        Return EditingControlFormattedValue
    End Function
    Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
        Me.Font = dataGridViewCellStyle.Font
    End Sub
    Public Property EditingControlRowIndex() As Integer Implements IDataGridViewEditingControl.EditingControlRowIndex
        Get
            Return rowIndexNum
        End Get
        Set(ByVal value As Integer)
            rowIndexNum = value
        End Set
    End Property
    Public Function EditingControlWantsInputKey(ByVal key As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInputKey
        Select Case key And Keys.KeyCode
            Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, Keys.Home, Keys.[End], _
             Keys.PageDown, Keys.PageUp, Keys.[Return]
                Return True
            Case Else
                Return False
        End Select
    End Function
    Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
        ' No preparation needs to be done.
    End Sub
    Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
        Get
            Return False
        End Get
    End Property
    Public Property EditingControlDataGridView() As DataGridView Implements IDataGridViewEditingControl.EditingControlDataGridView
        Get
            Return dataGridViewControl
        End Get
        Set(ByVal value As DataGridView)
            dataGridViewControl = value
        End Set
    End Property
    Public Property EditingControlValueChanged() As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged
        Get
            Return valueIsChanged
        End Get
        Set(ByVal value As Boolean)
            valueIsChanged = value
        End Set
    End Property
    Public ReadOnly Property EditingPanelCursor() As Cursor Implements IDataGridViewEditingControl.EditingPanelCursor
        Get
            Return MyBase.Cursor
        End Get
    End Property
#End Region '-- IDataGridViewEditingControl
End Class
Public Class ComboBoxAdvColumn 'CboAdvColumn
    Inherits DataGridViewComboBoxColumn
    Public Sub New()    'DVG_ComboBoxControl
        Me.CellTemplate = New ComboBoxAdvCell()
    End Sub
End Class
Public Class ComboBoxAdvCell   
    Inherits DataGridViewComboBoxCell
    Public Sub New()
    End Sub
    Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As System.Windows.Forms.DataGridViewCellStyle)
        ' Set the value of the editing control to the current cell value.
        MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
        Dim ctl As DVG_ComboBoxControl = TryCast(DataGridView.EditingControl, DVG_ComboBoxControl)
       If ctl IsNot Nothing Then
            ctl.SelectedIndex = 0
        End If
    End Sub
    '  Returns the type of the control that will be used for editing
    '  cells of this type.  This control must be a valid Windows Forms
    '  control and must implement IDataGridViewEditingControl.
    Public Overrides ReadOnly Property EditType() As Type
        Get
            Return GetType(DVG_ComboBoxControl)
        End Get
    End Property
End Class
Posted
Updated 2-Jul-11 16:41pm
v3
Comments
DaveAuld 2-Jul-11 15:43pm    
Edit: sorted formatting of code.
Homebunny 10-Apr-12 10:05am    
Have you been able to fix this? I'm also stuck in a simuliar DataGridViewComboBoxColumn.

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