ComboBox Control within DatagridView

94 Views Asked by At

I want to use DGV as a table to take User’s inputs. And in few columns, I want User to select values from a collection hence I decided to use Comboboxcontrol. I selected a standard DGV ("DGV2") and a standard Combobox ("CBTest") as tools. And I programmed this Combo to appear on Columnindex 2 (when user enters the particular cell, Combo box pops-up). User can select from the combo items (Drop-Down-List) and after selection clicked done, curser moves to next column. Now there is problems with UP/DOWN Keys when control is in cell having COMBO-BOX-

  1. I am not able to control behavior of UP /DOWN Keys when user enters the Cell having COMBO Control.
  2. Sometime with up-down keys cursor moves between Combobox items and sometimes it jumps in other rows. I am a beginner so any hint and help will be useful.

Code I used -

Public Class frmSIDDGV
    Dim nCol As Integer = 0
    Dim nRow As Integer = 0
    Private Sub frmSIDDGV_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       
        With DGV2
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
            .AutoResizeColumns()
            .RowTemplate.Height = 40
            .RowHeadersVisible = False
        End With
        DGV2.ColumnCount = 5

        With DGV2

            .Columns(0).Name = "Item"
            .Columns(1).Name = "MRP"
            .Columns(2).Name = "Qty"
            .Columns(3).Name = "Unit"
            .Columns(4).Name = "Amount"

        End With

        DGV2.Rows.Add()
        DGV2(0, 0).Value = 1

    End Sub

    Private Sub DGV2_KeyUp(sender As Object, e As KeyEventArgs) Handles DGV2.KeyUp

        If e.KeyCode = Keys.Enter Then
            
            If ActiveControl.Name <> "CBTest" Then

                If nCol = DGV2.ColumnCount - 1 Then
                    DGV2.Rows.Add()
                    DGV2.CurrentCell = DGV2(0, nRow + 1)
                Else
                    DGV2.CurrentCell = DGV2(nCol + 1, nRow)
                End If
            End If
        ElseIf e.KeyCode = Keys.Escape Then    
            If ActiveControl.Name = "CBTest" Then
                CBTest.SelectedIndex = 0 : CBTest.Visible = False

                DGV2.CurrentCell = DGV2(nCol, nRow)
                DGV2.Focus()
            End If
        End If
    End Sub

    Private Sub DGV2_CurrentCellChanged(sender As Object, e As EventArgs) Handles DGV2.CurrentCellChanged
        Try
            nCol = DGV2.CurrentCell.ColumnIndex
            nRow = DGV2.CurrentCell.RowIndex
        Catch ex As Exception
            nCol = 0
            nRow = 0
        End Try
    End Sub

    
    Private Sub DGV2_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DGV2.CellEnter
        Select Case e.ColumnIndex
            Case 2       ' User entered in Cell name "Item"

                DGV2.Controls.Add(CBTest)
                'DGV2.BeginEdit(True)
                Dim oRectangle = DGV2.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
                
                CBTest.Size = New Size(oRectangle.Width, oRectangle.Height)
                
                CBTest.Location = New Point(oRectangle.X, oRectangle.Y)
                CBTest.Visible = True
                SendKeys.Send("{F4}")               
                CBTest.SelectedIndex = 0
                CBTest.Capture = True
                CBTest.Focus()
        End Select
    End Sub

 
   Private Sub CBTest_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles CBTest.SelectionChangeCommitted
        With DGV2
            .Focus()            
            .Item(nCol, nRow).Value = Trim(CBTest.Text)
            .CurrentCell = .Rows(.CurrentRow.Index).Cells(nCol + 1)
        End With
        CBTest.Visible = False
    End Sub

End Class
0

There are 0 best solutions below