I have been trying to figure this out for a while and have found some answers, but I can't seem to make them work.

I have a datagridview bound to a datatable. I want replace one of the cells with a combo box and bind that combobox to one of the columns in the datatable. I have been able to replace the cell OK but currently have two issues I have not been able to resolve.

1. When I add items to the drop down (items.add ("Yes")...) they don't appear in the drop down box.
2. I can't figure out the syntax to bind a particular column in my datatable to the newColumn(combobox) in the datagridview.

Any help would be greatly appreciated!!

dbConnection = New OleDbConnection
        cmdCommand = New OleDbCommand
        Try
            dbConnection.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = Nosework.mdb"
            strSQL = "SELECT * FROM Competitors"
            cmdCommand.CommandText = strSQL
            cmdCommand.Connection = dbConnection



            dbConnection.Open()
            drDataReader = cmdCommand.ExecuteReader
            dtCompetitor = New DataTable
            dtCompetitor.Load(drDataReader)
            dbConnection.Close()


            dgInShow.DataSource = dtCompetitor
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Dim newColumn As New DataGridViewComboBoxColumn

        With newColumn
            .Name = "InShow"
            .HeaderText = "In Show"
            .Width = 50
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .ReadOnly = True
            .Visible = True
            .DefaultCellStyle.BackColor = Color.White
            .Items.Add("Yes")
            .Items.Add("No")
        End With
        dgInShow.Columns.Add(newColumn)

Recommended Answers

All 2 Replies

hello !
simple do this , first assign same datasource which you assign to your grid ,

grid.datasource = datasource 
combobox.datasource = datasource
'now here you can access any column
combobox.displaymember = "fieldname"

hope this will helps you

hello !
simple do this , first assign same datasource which you assign to your grid ,

grid.datasource = datasource 
combobox.datasource = datasource
'now here you can access any column
combobox.displaymember = "fieldname"

hope this will helps you

It didn't seem to do anything and even stranger is that I now have two combobox columns. It's not from the new code, I guess I just didn't see it before.

There's a screen shot here


Here's the modified code:

Public Sub filldgInShow()
        dbConnection = New OleDbConnection
        cmdCommand = New OleDbCommand
        Try
            dbConnection.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = Nosework.mdb"
            strSQL = "SELECT [ID],[First],[Last],[Dog],[Breed],[IsInShow] FROM Competitors"
            cmdCommand.CommandText = strSQL
            cmdCommand.Connection = dbConnection



            dbConnection.Open()
            drDataReader = cmdCommand.ExecuteReader
            dtCompetitor = New DataTable
            dtCompetitor.Load(drDataReader)
            dbConnection.Close()

 
            dgInShow.DataSource = dtCompetitor
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Dim row As DataRow

        row = dtCompetitor.Rows(dtIndex)
        'MsgBox(dtIndex)
        comboInShow.Text = row("IsInShow").ToString
        dgID.Text = row("ID").ToString

        newColumn = New DataGridViewComboBoxColumn

        With newColumn
            .Name = "InShow"
            .HeaderText = "In Show"
            .Width = 100
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
            .ReadOnly = True
            .Visible = True
            .DefaultCellStyle.BackColor = Color.White
            .Items.Add("Yes")
            .Items.Add("No")
            '.DataSource = dtCompetitor
            '.DisplayMember = "IsInShow"
        End With
        dgInShow.Columns.Add(newColumn)

        ' Adjust Column Widths
        dgInShow.Columns.Item(0).Width = 25
        dgInShow.Columns.Item(1).Width = 60
        dgInShow.Columns.Item(2).Width = 60
        dgInShow.Columns.Item(3).Width = 80
        dgInShow.Columns.Item(4).Width = 80
        'dgInShow.Columns.Item(5).Width = 60

        'Lock non-edit fields
        dgInShow.Columns.Item(0).ReadOnly = True
        dgInShow.Columns.Item(1).ReadOnly = True
        dgInShow.Columns.Item(2).ReadOnly = True
        dgInShow.Columns.Item(3).ReadOnly = True
        dgInShow.Columns.Item(4).ReadOnly = True
        'dgInShow.Columns.Item(5).ReadOnly = True

        dgInShow.Columns.Item(0).Visible = True
        dgInShow.Columns.Item(1).Visible = True
        dgInShow.Columns.Item(2).Visible = True
        dgInShow.Columns.Item(3).Visible = True
        dgInShow.Columns.Item(4).Visible = True
        dgInShow.Columns.Item(5).Visible = False



    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.