• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 629
  • Last Modified:

VB.net code to convert a DataGridView column to a ComboBox


I am using the following code to populate a DataGridview. How do I concert one of the columns to a ComboBox and add items to that

    Public oAccessAdapter_Accounts As OleDbDataAdapter
    Public oAccessTable_Accounts As New DataTable
    Public oSQLAdapter_Accounts As SqlDataAdapter
    Public oSQLTable_Accounts As New DataTable

    Private Sub Tasks_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Call Fill_DGV()
    End Sub

    Sub Fill_DGV()
            Dim sSQL As String

            sSQL = "Select * From Tasks"
            If My.Settings.DBType = "Access" Then
                Dim connection As New OleDbConnection(My.Settings.CS_Setting)
                oAccessAdapter_Accounts = New OleDbDataAdapter(sSQL, connection)
                Me.DataGridView1.DataSource = oAccessTable_Accounts
            ElseIf My.Settings.DBType = "SQL" Then
                Dim connection As New SqlConnection(My.Settings.CS_Setting)
                oSQLAdapter_Accounts = New SqlDataAdapter(sSQL, connection)
                Me.DataGridView1.DataSource = oSQLTable_Accounts
            End If
            Me.DataGridView1.Columns(0).Width = 40 'hide ID Column as this musn't be opened
            Me.DataGridView1.Columns(0).DefaultCellStyle.BackColor = Color.LightGray
            Me.DataGridView1.Columns(0).ReadOnly = True
        Catch ex As Exception
        End Try
    End Sub
Murray Brown
Murray Brown
  • 4
  • 3
1 Solution
Assuming your combobox is named "cmb":

cmb.datasource = oSQLTable_Accounts
cmb.valuemember = <columnname>
cmb.displaymember = <columnname>

(you can have one value as display and another as the "real" value of the ComboBox - which will be returned on cmb.value)
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi. I do not have a ComboBox columm before binding. I need to set the datasource then convert one of the columns from Text to ComboBox
I thought you meant you want to take one of the columns in the datagrid and copy it over to a combobox. Sorry.

I believe the simplest way would be to add a new DataGridViewTextBoxColumn to the datagridview and hide or delete the oldone. Even if it is possible to simply convert it, why would you want to go through that trouble?

You can populate the comboboxes either as a column or each cell individually.
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
YW. Need a snippet?
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Yes please. Thanks
Just above "End If"

Dim cmb As New DataGridViewComboBoxColumn()
cmb.HeaderText = "Header"
cmb.Name = "cmbBlahBlah"

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now