0

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
Rubén
  • 34,714
  • 9
  • 70
  • 166
Kirti
  • 1
  • 3
  • Because I couldn't be bothered writing out a whole answer, [here](https://www.vbforums.com/showthread.php?541476)'s one I prepared earlier. Note that that example binds a `DataTable` to the combo box column but you can bind anything that you would bind to a regular `ComboBox` control. You just have to make sure that you have properties/columns in the items that you can use to set the `DisplayMember` and `ValueMember`. That might be the same property/column for both but they would usually be different, with the value member representing a foreign key. – user18387401 Jul 14 '22 at 11:34
  • It is very difficult to understand what you are trying to do. Using a “regular” `ComboBox` in this context just seems odd to me. Is there some reason you do not use a `DataGridViewComboBoxColumn` as opposed to juggling a “regular” `ComboBox`? – JohnG Jul 14 '22 at 17:50
  • JohnG, there re 2 issues with DataGridViewComboBoxColumn - 1) it will look odd in my requirement . 2) Items of Combo I need to change based on entries done in some other column, hence I cannot use fixed combo items. I want to use combo for item selection and once Item is selected, selected value shall be copied to DGV Cell. – Kirti Jul 14 '22 at 19:04
  • Hmm… you state that 1) … _”it will look odd in my requirement”_ … ? … in what way will it look “odd”? What requirement is preventing you from using a combo box column? – JohnG Jul 14 '22 at 20:21
  • 2) … _”Items of Combo I need to change based on entries done in some other column, hence I cannot use fixed combo items.”_ … ? … Even though I see NO posted code that “changes” the combo box items… you CAN certainly have different values in each row’s combo box based on some other value in that row using a regular `DataGridViewComboBoxColumn`. I must be missing something and will assume you must go down this more complex path. Good Luck. – JohnG Jul 14 '22 at 20:21
  • @johng Actually this look is demand from Client. He do not want see Comboboxes permanently on screen. – Kirti Jul 15 '22 at 05:28
  • @user18387401 , thanks for reply But I do not want to use Combobox Column. Is there any other solution for this? – Kirti Jul 15 '22 at 05:31
  • Set the `DisplayStyle` of the column to `Nothing` and you won't see controls rendered except when editing a cell. – user18387401 Jul 15 '22 at 10:33
  • As noted already… set the columns `DisplayStyle` to `Nothing` or make your own custom Combo Box Column. It may help you in the future to put all the requirements into your question as opposed to getting peppered with unnecessary questions that could have been avoided if you supplied that info in your original question. Please peruse [How to ask](https://stackoverflow.com/help/how-to-ask) so you may avoid the unnecessary questions that only waste time for you and anyone else who tries to help. – JohnG Jul 15 '22 at 16:19
  • @JohnG , I am a beginner as I mentioned, will not say more. And for information for many other BEGINNERS, Trick that helped is "I removed the CODE LINE { DGV2.Controls.Add(CBTest)}. It is running smooth. And yes Suggestion of user18387401 is also fruitful. thanks everyone – Kirti Jul 15 '22 at 18:00
  • I am glad you got it working. My last comment was simply to help you in the future. You may have got your answer quicker if you had supplied all the requirements in your original question. I am aware you are new to this and my last comment was only a suggestion to make your experience here at SO more productive for you and anyone trying to help you. Good Luck. – JohnG Jul 15 '22 at 18:08
  • Also, to help you, you are aware that using a regular `ComboBox` has a feature which it not available in the grid’s combo box…. That feature is that the user can “type” whatever they want into the combo box. Therefore, the user can type anything into the combo box and that value will be set into the grids cell. So, technically you could end up with an inconsistent value in the cell that is NOT one of the items in the combo box. – JohnG Jul 15 '22 at 18:28
  • In addition, if the user clicks once into the combo box… then presses the “Enter” key… the code will throw an exception complaining about a re-entrant error into the current cell address. Just a heads up. – JohnG Jul 15 '22 at 18:28
  • @JohnG, appreciate your intention and sharing of useful information, thanks for taking time – Kirti Jul 15 '22 at 18:51

0 Answers0