on datagridview cellclick combobox

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
Hello peoples,

I have searched google for my question but couldn't find the right answer for it, so I hope someone here can help me.

My question is as followed:

I have a datagridview which gets it's columns on form load from a ms access database.
When a user clicks a cell in the first column, the cell should change into a combobox with items loaded from a file.

I know I can create a comboboxcolumn, but this is not what I want to do, I only want to have a combobox to a single cell and only in the first column.

Does anyone have an idea how to do this?

Thanks
 
After binding the data to the grid, you must create a DataGridViewComboBoxCell and add it to the grid in the appropriate position using the Item property of the grid itself. You can bind the cell in pretty much exactly the same way as you would bind a regular ComboBox.
 
Thanks, yeah I've came across some websites that did that thing, but neither did work, the way I hoped. Could you provide a sample?
 
As I say every time in situations like this, how about you show us what you tried and then we can help you fix it? For all we know you are almost there already, so our doing it all would be a waste. Also, the more you do yourself, the more beneficial it is.
 
Thanks,

I've been trying the following:

VB.NET:
Dim cmbCell As DataGridViewComboBoxCell
Dim isCombo As Boolean
Dim PointTypes As DataTable

Private Function CreateDataColumn(colType As String, name As String, caption As String, [readOnly] As Boolean) As DataColumn
        Dim column As New DataColumn()
        column.DataType = Type.[GetType](colType)
        column.ColumnName = name
        column.Caption = caption
        column.[ReadOnly] = [readOnly]
        Return column
    End Function

    Private Sub createTable()
        Try
            'cmbColumn source
            PointTypes = New DataTable("point_types")
            'create columns
            PointTypes.Columns.Add(CreateDataColumn("System.String", "PointType", "Point Type", True))
            Dim cmd3 As OleDbCommand = New OleDbCommand("SELECT Songtitle FROM songs ORDER BY songtitle ASC", con)
            con.Open()
            Dim sdr As OleDbDataReader = cmd3.ExecuteReader
            PointTypes.Rows.Add("Select Song...")
            While sdr.Read()
                PointTypes.Rows.Add(sdr.Item("Songtitle").ToString)
            End While
            con.Close()
            'PointTypes.SelectedIndex = 0
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Source & ": " & ex.Message, "Songlist Editor 2 Error !!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub AudioCheckBox_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles AudioCheckBox.CheckedChanged
        If AudioCheckBox.Checked Then
            AudioGrid.Enabled = True
            AudioAddEditButton.Enabled = True
            createTable()
            Try
                Dim cmd2 As OleDbCommand = New OleDbCommand("SELECT * FROM Audio", con)
                con.Open()
                Dim mydat As OleDbDataAdapter = New OleDbDataAdapter(cmd2)
                dsOle = New DataSet
                mydat.Fill(dsOle, "Audio")
                con.Close()
                AudioGrid.DataSource = dsOle.Tables("Audio")
                Me.AudioGrid.Columns("Id").Visible = False
                Me.AudioGrid.Columns("Songtext").Visible = False
            Catch ex As Exception
                MessageBox.Show("Error: " & ex.Source & ": " & ex.Message, "Songlist Editor 2 Error !!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End Try
        Else
            AudioGrid.Enabled = False
            AudioAddEditButton.Enabled = False
        End If
    End Sub

Private Sub AudioGrid_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles AudioGrid.CellClick
        If e.ColumnIndex = AudioGrid.Columns("Songtitle").Index Then
            If cmbCell Is Nothing Then
                cmbCell = New DataGridViewComboBoxCell()
                cmbCell.DataSource = PointTypes
                AudioGrid(e.ColumnIndex, e.RowIndex) = cmbCell
                cmbCell.ValueMember = PointTypes.Columns("Songtitle").ColumnName
                cmbCell.DisplayMember = PointTypes.Columns("Songtitle").ColumnName
                isCombo = True
            End If
        End If
    End Sub

    Private Sub AudioGrid_CellValidating(sender As Object, e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles AudioGrid.CellValidating
        If e.ColumnIndex = AudioGrid.Columns("Songtitle").Index Then
            If isCombo Then
                isCombo = False
                cmbCell = Nothing
                AudioGrid(e.ColumnIndex, e.RowIndex) = New DataGridViewTextBoxCell()
            End If
        End If
    End Sub

With this code I get the following error:
Object reference not set to an instance of an object.

This error is related to the following line:
cmbCell.ValueMember = PointTypes.Columns("Songtitle").ColumnName

Thanks for any help.
 
Well that's kinda easy fixed. Which reference on that line is Nothing and where exactly did you expect a value to be assigned? There's only three possibilities, i.e. anything before a dot.
 
Back
Top