Click here to Skip to main content
15,897,273 members
Articles / Programming Languages / Visual Basic

Embedding a DataGridView in a ComboBox

Rate me:
Please Sign up or sign in to vote.
4.95/5 (18 votes)
8 May 2013CPOL5 min read 68.7K   3.6K   61  
A simple way to place any DataGridView inside a ComboBox.
Imports System.Windows.Forms
Public Class AccGridComboBoxEditingControl
    Inherits AccGridComboBox
    Implements IDataGridViewEditingControl

    Public Sub New()
        TabStop = False ' control must not be part of the tabbing loop ???
    End Sub


    Private _dataGridView As DataGridView
    Public Property EditingControlDataGridView() As DataGridView _
        Implements IDataGridViewEditingControl.EditingControlDataGridView
        Get
            Return _dataGridView
        End Get
        Set(ByVal value As System.Windows.Forms.DataGridView)
            _dataGridView = value
        End Set
    End Property

    Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
        Get
            Return GetEditingControlFormattedValue(DataGridViewDataErrorContexts.Formatting)
        End Get
        Set(ByVal value As Object)
            Me.SelectedItem = value
        End Set
    End Property

    Private _rowIndex As Integer
    Public Property EditingControlRowIndex() As Integer Implements IDataGridViewEditingControl.EditingControlRowIndex
        Get
            Return _rowIndex
        End Get
        Set(ByVal value As Integer)
            _rowIndex = value
        End Set
    End Property

    Private _hasValueChanged As Boolean = False
    Public Property EditingControlValueChanged() As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged
        Get
            Return _hasValueChanged
        End Get
        Set(ByVal value As Boolean)
            _hasValueChanged = value
        End Set
    End Property

    Public ReadOnly Property EditingPanelCursor() As Cursor Implements IDataGridViewEditingControl.EditingPanelCursor
        Get
            Return MyBase.Cursor
        End Get
    End Property

    Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
        Get
            Return False
        End Get
    End Property

    Protected Overrides ReadOnly Property DisposeToolStripDataGridView() As Boolean
        Get
            Return False
        End Get
    End Property



    Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) _
            Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
        Me.Font = dataGridViewCellStyle.Font
        Me.BackColor = dataGridViewCellStyle.BackColor
        Me.ForeColor = dataGridViewCellStyle.ForeColor
    End Sub

    Private Sub SelectedValueChangedHandler(ByVal sender As Object, ByVal e As EventArgs) Handles Me.SelectedValueChanged
        If Not _hasValueChanged Then
            _hasValueChanged = True
            _dataGridView.NotifyCurrentCellDirty(True)
        End If
    End Sub

    Public Function EditingControlWantsInputKey(ByVal keyData As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInputKey
        Select Case keyData And Keys.KeyCode
            Case Keys.Up, Keys.Down, Keys.PageDown, Keys.PageUp, Keys.Enter, Keys.Escape, Keys.Delete
                Return True
            Case Else
                Return False
        End Select
    End Function

    Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object Implements System.Windows.Forms.IDataGridViewEditingControl.GetEditingControlFormattedValue
        Return Me.Text
    End Function

    Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit

    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Business Analyst Linden
Lithuania Lithuania
I'm a lawyer in a law firm. Programing is my hobby.

Comments and Discussions