Thanks Bart and Cor that did the trick,
I have used your suggestion with a slight alteration that enables me to make
use of the strong typing of the InvoicesDataRow object.
Dim drv As DataRowView = DirectCast(_
InvoicesBindingSource.AddNew(), _
DataRowView)
Dim dr As MainDataSet.InvoicesRow = DirectCast(drv.Row,
MainDataSet.InvoicesRow)
dr.CustID = 4
dr.InvoiceDate = Date.Today
drv.EndEdit()
InvoicesTableAdapter.Update(MainDataSet.Invoices)
Thanks once again, this is exactly what I was trying to achieve.
"Bart Mermuys" <bm*************@hotmail.com> wrote in message
news:uo****************@TK2MSFTNGP10.phx.gbl...
Hi,
"Martin Horn" <mv****@theinternet.com> wrote in message
news:Df******************@newsfe2-gui.ntli.net... Hi all,
I am using VB2005 and the new data controls, which all work fine, except
there is a situation where I want to add a new row to the database
without using the 'AddNewItem' button on the BindingNavigator control; it
has to be done programatically.
This is the best I can come up with but it fails on line 3 with error
'Objects added to a BindingSource's list must all be of the same type.'
Dim dt As New MainDataSet.InvoicesDataTable
Dim dr As MainDataSet.InvoicesRow = dt.NewInvoicesRow
InvoicesBindingSource.Add(dr) <Error here>
' Set field values here
dr.field1 = 1
dr.field2 = ...etc
' Save the new row to the database
InvoicesBindingSource.EndEdit()
InvoicesTableAdapter.Update(MainDataSet.Invoices)
Like Cor suggest you can add the new DataRow directly to the underlying
DataTable. The new record will be added to the UI, but the BindingSource
will not position to the new record:
Dim row As MainDataSet.InvoicesRow = _
MainDataSet.Invoices.NewInvoicesRow()
row.field1 = ...
row.field2 = ...
MainDataSet.Invoices.Rows.Add(row)
InvoicesTableAdapter.Update(MainDataSet.Invoices)
--Or you can add a new row using the BindingSource and a DataRowView, this
way the BindingSource will position to the new record:
Dim drv As DataRowView = DirectCast( _
InvoicesBindingSource.AddNew(), _
DataRowView)
drv("field1") = ...
drv("field2") = ...
drv.EndEdit()
InvoicesTableAdapter.Update(MainDataSet.Invoices)
HTH,
Greetings
Dim dr As MainDataSet.InvoicesRow = dt.NewInvoicesRow
InvoicesBindingSource.Add(dr) <Error here>
' Set field values here
dr.field1 = 1
dr.field2 = ...etc
' Save the new row to the database
InvoicesBindingSource.EndEdit()
InvoicesTableAdapter.Update(MainDataSet.Invoices)
Any advice will be appreciated.
Thanks very much,
Martin Horn.