Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound

  • Thread starter Thread starter Apostolos Doudakmanis
  • Start date Start date
A

Apostolos Doudakmanis

Guest
Hi

I wrote this code to add a new row in datagridview

Sub AddNewRow()
OpenDB()

ds = New DataSet
tables = ds.Tables
da = New MySqlDataAdapter("Select * from ETAB10", myConnection2) 'Change items to your database name
da.Fill(ds, "ETAB10") 'Change items to your database name
Dim view As New DataView(tables(0))
source1.DataSource = view

DataGridView1.DataSource = view
DataGridView1.Columns(0).HeaderText = "Code"
DataGridView1.Columns(1).HeaderText = "Details"
DataGridView1.Columns(1).Width = 200

'------------------------------------------------------------------------------------
Dim previousAllowUserToAddRows = DataGridView1.AllowUserToAddRows

Dim nRow As DataRow ' New Row in the table
nRow = view.Table.NewRow
nRow.Item("ID") = DataGridView1.Rows.Count ' Autonumber +1 (0..n+1)
nRow.Item("f1") = "Sample Works" ' Text
view.Table.Rows.Add(nRow)

DataGridView1.AllowUserToAddRows = True ' New Row in the DataGridView
DataGridView1.Rows.Add()
DataGridView1.AllowUserToAddRows = previousAllowUserToAddRows

DataGridView1.Refresh()
'------------------------------------------------------------------------------------

CloseDB()
End Sub

But i get the message "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound"

i don't understand where is the mistake

Continue reading...
 
Back
Top