1

I'm using vb.NET in Visual Studio 2010. I found an example of how to add a ComboBox to a DataGridView cell, and I added it to my code. When I run the code, and I add a new row, the ComboBox is visible, but it has no value displayed in it and it won't drop down.

Have I missed something from the code? Does the DataGridView need to have certain properties set?

dgvFiles.Rows.Add({"Cell1","Cell2"})
Dim gridComboBox As New DataGridViewComboBoxCell
gridComboBox.Items.Add("A") 'Populate the Combobox
gridComboBox.Items.Add("B") 'Populate the Combobox
gridComboBox.Items.Add("C") 'Populate the Combobox
dgvFiles(2, dgvFiles.Rows.Count - 1) = gridComboBox

Edit:

I had set four columns at design time, that wasn't the issue. The issue turned out to be that I had set the DataGridView to 'EditProgrammatically'. I had changed it to that initially to stop users from editing the text cells, but apparently, it prevented the ComboBoxes from dropping.

I appreciate all the answers given. My apologies that I forgot to mention that I had set four columns in design time, and that this issue was caused by me not realising the EditProgrammatically setting had this effect.

jcvamp
  • 25
  • 8
  • I believe you have to add a datagridviewcomboboxcolumn and add the comboboxcell to that column. Like here: https://stackoverflow.com/questions/11657345/how-do-i-select-a-value-in-a-datagridviewcomboboxcell – Jacob H Sep 20 '17 at 12:40
  • You could also do all of that setup in design mode instead of using code if your values are static as in your example. – braX Sep 20 '17 at 12:48
  • you don't have to make the entire column like jacob said. you could have just 1 combobox in the grid. –  Sep 20 '17 at 12:50

1 Answers1

0

Your code is almost fine. Everything drops down. You could have the default value displayed on your list.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dgvfiles.Columns.Add("Column1", "Column 1")
        dgvfiles.Columns.Add("Column2", "Column 2")
        dgvfiles.Columns.Add("Column3", "Column 3")
        dgvfiles.Columns.Add("Column4", "Column 4")
        dgvfiles.Rows.Add({"Cell1", "Cell2"})
        Dim gridComboBox As New DataGridViewComboBoxCell
        gridComboBox.Items.Add("A") 'Populate the Combobox
        gridComboBox.Items.Add("B") 'Populate the Combobox
        gridComboBox.Items.Add("C") 'Populate the Combobox
        gridComboBox.Value = gridComboBox.Items(0)
        dgvfiles(2, dgvfiles.Rows.Count - 2) = gridComboBox
    End Sub

    Private Sub dgvfiles_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles dgvfiles.CellBeginEdit
        If e.ColumnIndex = 2 Then
            'Do something
        Else
            e.Cancel = True
        End If
    End Sub

enter image description here

  • But the DataGridView is set up with four columns in design mode. – jcvamp Sep 20 '17 at 14:09
  • Adding gridComboBox.Value = gridComboBox.Items(0) makes it so that a value is displayed, but it still won't let me drop down. I'm going to make a test project with a blank DataGridView and see if it works on that, so that I can see if I've changed a setting at design time that's screwing it up. – jcvamp Sep 20 '17 at 14:15
  • Okay, apparently, it wasn't working because I had it set to 'EditProgrammatically'. But changing that to anything else makes the other cells editable, which I didn't want. I guess, I'm going to have to find a way to make the other cells uneditable. – jcvamp Sep 20 '17 at 14:26
  • I never expected you to read my mind, I just forgot to say how many columns I had set at design time. When you said 'you are adding only 2 column', I realised that I hadn't mentioned it, so I replied saying that I had set it up with four columns. The part about needing to edit the combobox and not the other cells didn't seem relevant, because I didn't know that EditProgrammatically would stop comboboxes from working, and regardless of your statement that changing the grid to editprogrammatically doesn't affect it, that's EXACTLY what happened in my program. – jcvamp Sep 20 '17 at 14:51
  • @jcvamp add this to beginedit and set your grid to editonkeystoke –  Sep 20 '17 at 14:53