Question Combobox in one cell of newly appended rows

recycler

New member
Joined
Jul 13, 2008
Messages
2
Programming Experience
5-10
Context VB .NET 2008 Express Edition....

I use a binding to fill say two rows of a datagridview, each having four text cells. So far so so good.

But I also want the user to be able to append more than one new rows. In order to ensure that the user only enters a permitted text item, I want to replace one of the default 'text boxes' with a combobox.

By using the DefaultValuesNeeded event, I can insert a pre-prepared DataGridViewComboBoxCell in one appended row. But when I try to append a second new row, I get the error
"InvalidOperationException was unhandled. Cell provided already belongs to a grid. This operation is not valid."

The code below is a minimised version of what I use

VB.NET:
Public Class Form1
    Dim myOrdersTable As New DataTable
    Dim myAdapter As New MySqlDataAdapter
    Dim dgvcc As New DataGridViewComboBoxCell

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      			' ..............................
                        myAdapter.Fill(myOrdersTable)
                        With DataGridView1
                            .DataSource = myOrdersTable
                            .AutoResizeRows()
                        End With
				' ...........................
        MakeDGVCbx()
    End Sub
    
    Sub MakeDGVCbx()
        Dim myData As New DataTable
			' ........................
                        myAdapter.Fill(myData)
                        dgvcc.DataSource = myData
                        dgvcc.DisplayMember = "Item"
                        dgvcc.ValueMember = "Item"
			' .......................
    End Sub

    Private Sub DataGridView1_DefaultValuesNeeded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) _
Handles DataGridView1.DefaultValuesNeeded
        Dim thisone As Object
        thisone = dgvcc
        With e.Row
           ' ........................... other default values
            .Cells("Item") = dgvcc
        End With
    End Sub
    
End Class

I think I'm getting confused about declaration and instantiation of the DataGridViewComboBoxCell and would be grateful for guidance on how to correct this issue.

Thank you.
 
Back
Top