2

I have a DatagridviewComboBoxColumn populated from a DataTable and whenever I click on any part of the DataGridViewComboBoxCell the first value of the list shows up as it had been clicked. However, when I move the focus to another cell without selecting a value, it disappears.

Strangely, the behavior is not consistent if I apply the ComboBox values with .Items.Add(" "). Can anyone shed some light on this issue. Here is a sample code and a gif image:

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DataGridView1.Rows.Add()
    DataGridView1.Rows.Add()

    ''DataGridViewComboBoxColumn1
    DataGridViewComboBoxColumn1.Items.Add("Name1")
    DataGridViewComboBoxColumn1.Items.Add("Name2")

    'DataGridViewComboBoxColumn2
    Dim dt As New DataTable
    dt.Columns.Add("id")
    dt.Columns.Add("name")
    dt.Rows.Add("1", "Name1")
    dt.Rows.Add("2", "Name2")
    With DataGridViewComboBoxColumn2
        .ValueMember = dt.Columns(0).ColumnName
        .DisplayMember = dt.Columns(1).ColumnName
        .DataSource = dt
    End With
End Sub

End Class

enter image description here

  • It's sounds like a "usual" behavior. It must be clicked. – noidea Jun 23 '16 at 10:54
  • The ComboboxColumns have the same settings but are populated in a different manner. Column1 doesn't show any values until the dropdown menu is displayed. While Column2 which is populated through .DataSource flashes the first value before even the dropdown menu is triggered. As I have stated: it only flashes it but doesn't select it. – Stanislav Lazarov Jun 23 '16 at 11:39
  • I've tested it and it works fine. But you have to click the item you want, not just "select" it.. It's a user problem, not a bad behavior. – noidea Jun 23 '16 at 13:31

2 Answers2

0

Successfully replicated the behavior, but i also added 2 ComboBox to the same form and the result is same

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        DataGridView1.Rows.Add()
        DataGridView1.Rows.Add()

        ''DataGridViewComboBoxColumn1


        DataGridViewComboBoxColumn1.Items.Add("Name1")
        DataGridViewComboBoxColumn1.Items.Add("Name2")

        'DataGridViewComboBoxColumn2
        Dim dt As New DataTable
        dt.Columns.Add("id")
        dt.Columns.Add("name")
        dt.Rows.Add("1", "Name1")
        dt.Rows.Add("2", "Name2")
        With DataGridViewComboBoxColumn2
            .ValueMember = dt.Columns(0).ColumnName
            .DisplayMember = dt.Columns(1).ColumnName
            .DataSource = dt
        End With

        ComboBox1.Items.Add("Name1")
        ComboBox1.Items.Add("Name2")

        With ComboBox2
            .ValueMember = dt.Columns(0).ColumnName
            .DisplayMember = dt.Columns(1).ColumnName
            .DataSource = dt
        End With

    End Sub
End Class

Click to see gif of form in action

  • As you can see Name1 appears in Column2 without you actually selecting it whereas in the case of Column1 it remains blank. My question is how to have the same behavior as in Column1 while using a datasource as it might confuse users. – Stanislav Lazarov Jun 23 '16 at 14:27
0

I found a way to correct the behavior by referring to this post: DataGridComboBoxColumn shows first value on CellEnter

Here is the code I have used:

    Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
      If TypeOf e.Control Is ComboBox Then
          Dim comboBox As ComboBox = DirectCast(e.Control, ComboBox)
          If DataGridView1.CurrentCell.Value Is Nothing Then comboBox.SelectedIndex = -1
      End If
    End Sub
Community
  • 1
  • 1