I have a form with two datagridview that gets items from a mysql database.
My goal is to click an item of the first DGV and get it inserted into the second, then (pressing a button) update the table on the database.
The problem is that I cannot add a row (I get exception: Can not add rows programmatically to the set of rows in DataGridView when data-bound control) and, if I programmatically fill the last row (the row for manual inserting) the value is not acquired.
But if I fill manually the last row and press the "update button" the database become updated.
This is the code of "CellContentClick"
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim DvgRow% = e.RowIndex
Dim DvgCol% = e.ColumnIndex
Dim SelCell$ = Me.DataGridView1.Item(DvgCol, DvgRow).Value.ToString.Trim()
Dim IntColSel$ = Me.DataGridView1.Columns(DvgCol).HeaderText
Dim Dgv2Rows% = Me.DataGridView2.RowCount
If IntColSel.ToUpper = "REGION" Then
'Here I get the value in the last row but is ignored being updated
Me.DataGridView2.Item(0, Me.DataGridView2.RowCount - 1).Value = SelCell
'Here I get exception
Me.DataGridView2.Rows.Add()
End If
End Sub
This is the code to fill the 2nd DGV:
Private Sub GetInDGV2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
With DataGridView2
.DataSource = Nothing
.Rows.Clear()
.Columns.Clear()
End With
cnString = "datasource=" + Server_Name + ";username= " + UserDB + _
";password=" + Password + ";database=" + Database_Name + ""
sqlQRY2 = "Select Region from regions"
conn = New MySqlConnection(cnString)
Try
conn.Open()
da2 = New MySqlDataAdapter(sqlQRY2, conn)
Dim cb2 As MySqlCommandBuilder = New MySqlCommandBuilder(da2)
da2.Fill(ds2, "regions")
DataGridView2.DataSource = ds2
DataGridView2.DataMember = "regions"
Catch ex As Common.DbException
MsgBox(ex.ToString)
Finally
conn.Close()
End Try
End Sub
I saw this question aswered but I wasn't able to translate from C an test it.
Thanks for any help.