Click here to Skip to main content
15,893,588 members

How keep showing actual item of grid-combo when focused ?

jdebabrata asked:

Open original thread
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
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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