Solved code to convert a DataGridView column to a ComboBox

Posted on 2012-08-23
Last Modified: 2012-08-23

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
Question by:murbro
    LVL 2

    Expert Comment

    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)

    Author Comment

    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
    LVL 2

    Accepted Solution

    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.

    Author Closing Comment

    LVL 2

    Expert Comment

    YW. Need a snippet?

    Author Comment

    Yes please. Thanks
    LVL 2

    Expert Comment

    Just above "End If"

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

    Open in new window


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now