none
How to set combobox in datagridview in visual basic?

    Question

  • Hi Guys.

    I  have a data grid view with data bound.

    So i would like to edit the data inside the datagridview.

    but i want to edit the data inside the datagridview cell by choosing combobox with dataset.

    So how can i add the combobox into datagridview current cell with data bound?

    Thank you.

     


    Tuesday, September 20, 2011 5:43 AM

Answers

  • Hi brownie,

    Assuming that you are using sql database, so using Northwind, try this code below on how to populate combobox column of dgv that is bound to a database:

    Imports System.Data.SqlClient
    Public Class Form1
        Private CompanyCode As DataGridViewComboBoxColumn
        Dim CompanyName, ContactName As DataGridViewTextBoxColumn
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.CompanyCode = New DataGridViewComboBoxColumn()
            Me.CompanyCode.Name = "CompanyCode"
            Me.CompanyCode.HeaderText = "Company Code"
            Me.DataGridView1.Columns.Add(Me.CompanyCode)
    
            Me.CompanyName = New DataGridViewTextBoxColumn()
            Me.CompanyName.Name = "CompanyName"
            Me.CompanyName.HeaderText = "Company Name"
            Me.DataGridView1.Columns.Add(Me.CompanyName)
    
            Me.ContactName = New DataGridViewTextBoxColumn()
            Me.ContactName.Name = "ContactName"
            Me.ContactName.HeaderText = "Contact Name"
            Me.DataGridView1.Columns.Add(Me.ContactName)
    
            populateComboFromDb()
        End Sub
        Private Sub populateComboFromDb()
            Dim conConnect As New SqlConnection("Data Source = (local); Database = 'Northwind'; Integrated Security = True")
            Try
                Dim sqlId As String = "Select CustomerID From Customers;"
                Dim dAdapter As New SqlDataAdapter(sqlId, conConnect)
                Dim dt As New DataTable()
                dAdapter.Fill(dt)
                CompanyCode.DataSource = dt
                CompanyCode.ValueMember = "CustomerID"
                CompanyCode.DisplayMember = "CustomerID"
            Catch
            End Try
        End Sub
    End Class
    
    

    Hope it helps.

    Thanks.

    Hardz

    Monday, September 26, 2011 1:14 AM

All replies

  • Tuesday, September 20, 2011 6:42 AM
  • Thank you for your reply. but that is not the answer i m looking for..
    Tuesday, September 20, 2011 6:51 AM
  • Hi Brownie,

    Welcome to the MSDN Forum.

    I have changed this thread type to "question", because you have asked a question.

    Here is sample from Cor's website: http://www.vb-tips.com/DataGridViewComboFromDataTableSimple.aspx.ASPX

    And this is the code:

            Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                Dim dt As New DataTable("Names")
                dt.Columns.Add("Name")
                dt.Columns.Add("State")
                dt.LoadDataRow(New Object() {"Ken Tucker", "Florida"}, True)
                dt.LoadDataRow(New Object() {"Cor Ligthert", "Netherlands"}, True)
                dt.LoadDataRow(New Object() {"John Antonny Oliver", "England"}, True)
                dt.LoadDataRow(New Object() {"Armin Zignler", "Germany"}, True)
                dt.LoadDataRow(New Object() {"Paul Clement", "Illinois"}, True)
                dt.LoadDataRow(New Object() {"Liliane Teng", "SjangHai"}, True)
                DataGridView1.DataSource = dt
                DataGridView1.AllowUserToAddRows = True
                DataGridView1.Columns.Remove("State")
                Dim dtCombo = dt.Copy
                dtCombo.TableName = "States"
                Dim dgvCombo As New DataGridViewComboBoxColumn
                dgvCombo.DataSource = dtCombo
                dgvCombo.DisplayMember = "State"
                dgvCombo.ValueMember = "Name"
                dgvCombo.DataPropertyName = "Name"
                DataGridView1.Columns.Add(dgvCombo)
            End Sub
        End Class
    

    I hope this will be  helpful.

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, September 23, 2011 3:17 AM
  • thnx for ur reply. is a gd post. but is not my ans because my datagridview is databound. so cant match with ur post..
    Monday, September 26, 2011 12:29 AM
  • Hi brownie,

    Assuming that you are using sql database, so using Northwind, try this code below on how to populate combobox column of dgv that is bound to a database:

    Imports System.Data.SqlClient
    Public Class Form1
        Private CompanyCode As DataGridViewComboBoxColumn
        Dim CompanyName, ContactName As DataGridViewTextBoxColumn
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.CompanyCode = New DataGridViewComboBoxColumn()
            Me.CompanyCode.Name = "CompanyCode"
            Me.CompanyCode.HeaderText = "Company Code"
            Me.DataGridView1.Columns.Add(Me.CompanyCode)
    
            Me.CompanyName = New DataGridViewTextBoxColumn()
            Me.CompanyName.Name = "CompanyName"
            Me.CompanyName.HeaderText = "Company Name"
            Me.DataGridView1.Columns.Add(Me.CompanyName)
    
            Me.ContactName = New DataGridViewTextBoxColumn()
            Me.ContactName.Name = "ContactName"
            Me.ContactName.HeaderText = "Contact Name"
            Me.DataGridView1.Columns.Add(Me.ContactName)
    
            populateComboFromDb()
        End Sub
        Private Sub populateComboFromDb()
            Dim conConnect As New SqlConnection("Data Source = (local); Database = 'Northwind'; Integrated Security = True")
            Try
                Dim sqlId As String = "Select CustomerID From Customers;"
                Dim dAdapter As New SqlDataAdapter(sqlId, conConnect)
                Dim dt As New DataTable()
                dAdapter.Fill(dt)
                CompanyCode.DataSource = dt
                CompanyCode.ValueMember = "CustomerID"
                CompanyCode.DisplayMember = "CustomerID"
            Catch
            End Try
        End Sub
    End Class
    
    

    Hope it helps.

    Thanks.

    Hardz

    Monday, September 26, 2011 1:14 AM