Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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.
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 2-Jul-11 8:57am
Edited 2-Jul-11 17:41pm
v3
Comments
DaveAuld at 2-Jul-11 15:43pm
   
Edit: sorted formatting of code.
Homebunny at 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)

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 295
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 2 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100