0

I want to populate a DataGridViewComboBoxColumn with a list of strings using vb.net. The DataGridView contains other columns which are very important to display as well.

Blackwood
  • 4,504
  • 16
  • 32
  • 41
Tassisto
  • 9,877
  • 28
  • 100
  • 157

1 Answers1

0

Perhaps this might assist you

''' <summary>
''' This example uses no backend data source.
''' 
''' Two controls, DataGridView named DataGridView1
''' and one Label named Label1
''' 
''' DataGridView has two columns created in the IDE
''' Column1 - DataGridViewComboBoxColumn
'''           * DisplayStyle = DataGridViewComboBoxDisplayStyle.
'''             Nothing set in the DataGridView Columns editor
''' Column2 - DataGridViewTextBoxColumn, nothing changed in settings
''' 
''' </summary>
''' <remarks></remarks>
Public Class Form2
    Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

        CType(DataGridView1.Columns("Column1"), DataGridViewComboBoxColumn).DataSource = _
            New String() {"Add", "Subtract", "Divide", "Multiply", "Total"}

        DataGridView1.Rows.Add(New Object() {"Divide", "B1"})
        DataGridView1.Rows.Add(New Object() {"Add", "B2"})
        DataGridView1.Rows.Add(New Object() {"Subtract", "B3"})
        DataGridView1.Rows.Add(New Object() {"Add", "B4"})
        DataGridView1.Rows.Add(New Object() {"Multiply", "B5"})
    End Sub
    ''' <summary>
    ''' If the event is triggered by the DataGridViewComboBoxColumn cast
    ''' e.Control to a ComboBox and setup SelectionChangedCommitted event
    ''' so that when the current cell value changes we can get it.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DataGridView1_EditingControlShowing( _
        ByVal sender As Object, _
        ByVal e As DataGridViewEditingControlShowingEventArgs) _
    Handles DataGridView1.EditingControlShowing

        If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name = "Column1" Then
            Dim cb As ComboBox = TryCast(e.Control, ComboBox)
            RemoveHandler cb.SelectionChangeCommitted, AddressOf _SelectionChangeCommitted
            AddHandler cb.SelectionChangeCommitted, AddressOf _SelectionChangeCommitted
        End If

    End Sub
    Private Sub _SelectionChangeCommitted(sender As Object, e As EventArgs)
        Label1.Text = CType(sender, DataGridViewComboBoxEditingControl).Text
    End Sub
    Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        If DataGridView1.Columns(e.ColumnIndex).Name = "Column1" Then
            If IsComboBoxCell(DataGridView1(e.ColumnIndex, e.RowIndex)) Then
                SendKeys.Send("{F4}")
            End If
        End If
    End Sub
    Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        AddHandler DataGridView1.CellEnter, AddressOf DataGridView1_CellEnter
    End Sub
    Public Function IsComboBoxCell(ByVal sender As DataGridViewCell) As Boolean
        If sender.EditType IsNot Nothing Then
            Return (sender.EditType Is GetType(DataGridViewComboBoxEditingControl))
        Else
            Return False
        End If
    End Function
End Class
Karen Payne
  • 4,341
  • 2
  • 14
  • 31