[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 601
  • Last Modified:

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

Hi

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()
        Try
            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)
                oAccessAdapter_Accounts.Fill(oAccessTable_Accounts)
                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)
                oSQLAdapter_Accounts.Fill(oSQLTable_Accounts)
                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
            MsgBox(ex.Message)
        End Try
    End Sub
0
Murray Brown
Asked:
Murray Brown
  • 4
  • 3
1 Solution
 
BaleboosCommented:
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)
0
 
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
0
 
BaleboosCommented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Dim cmb As New DataGridViewComboBoxColumn()
cmb.HeaderText = "Header"
cmb.Name = "cmbBlahBlah"
cmb.Items.Add("item1")
cmb.Items.Add("item2")
....
DataGridView1.Columns.Add(cmb)

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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