Sunday, 19 July 2015

How to add Bounded CombBox into Datagridview

Private Sub DisplayItems()
        Dim vSqlStr As String = "SELECT * FROM ItemMaster"
        Dim vSqlCat As String = "SELECT * FROM ItemCat"

        Using vDA As New OleDb.OleDbDataAdapter(vSqlStr, con)
            Using vDs As New DataSet
                vDA.Fill(vDs, "ItemMaster")

                Me.dgvItemList.DataSource = vDs.Tables("ItemMaster")
            End Using
        End Using

        'Formatting Columns

        Try
            If Not Me.dgvItemList Is Nothing AndAlso Not Me.dgvItemList.CurrentRow Is Nothing Then
                Me.dgvItemList.Columns(0).Visible = False
                Me.dgvItemList.Columns(3).Visible = False

                Me.dgvItemList.ColumnHeadersDefaultCellStyle.Font = New Font(Me.dgvItemList.Font, FontStyle.Bold)
                Me.dgvItemList.Columns(1).HeaderText = "Item Name"
                Me.dgvItemList.Columns(1).Width = 160
                Me.dgvItemList.Columns(2).HeaderText = "Item Price"

                'add combo box
                Dim cbo As New DataGridViewComboBoxColumn

                cbo.DataPropertyName = "ItemCatID"
                cbo.HeaderText = "Category"
                cbo.Name = "cboCatID"

                Using vDA As New OleDb.OleDbDataAdapter(vSqlCat, con)
                    Using vDs As New DataSet
                        vDA.Fill(vDs, "ItemCat")

                        cbo.DataSource = vDs.Tables("ItemCat")
                    End Using
                End Using

                cbo.DisplayMember = "ItemCatName"
                cbo.ValueMember = "ItemCatID"

                Me.dgvItemList.Columns.Add(cbo)
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
     
        End Try
    End Sub