Populating a Table programmatically
Hi.
I'm working in VB 2008 Express.
I have an MS Access DB with a Table setup with the columns that I need.
I have it linked into a form, with the requisite BindingSource, TableAdapter, BindingNavigator, etc.
On the form, I also have several textboxes linked back to the table. I can populate the table via the + (add new) button in the BindingNavigator and then filling in the textboxes (and saving of course).
Is there a way that I can put a button on the form and populate the table automatically? I want to do this via a textbox to control the number of records added - that way, I can put in an 8 and have 8 blank records added to the table via this one click (and don't have to enter the default info in for the 8 records one at a time).
I've attempted to do this by calling the BindingNavigatorAddNewItem_Click subroutine, filling in the textboxes and then calling MapBindingNavigatorSaveItem_Click subroutine, but this does not work.
Any suggestions?
Thanks,
Ken
Re: Populating a Table programmatically
If you want to add N rows then you simply create N DataRows and add them to the DataTable. That would involve calling NewRow and Rows.Add N times. You won't be able to add a row with no data if any of your columns are non-nullable though.
Re: Populating a Table programmatically
Quote:
Originally Posted by
jmcilhinney
If you want to add N rows then you simply create N DataRows and add them to the DataTable. That would involve calling NewRow and Rows.Add N times. You won't be able to add a row with no data if any of your columns are non-nullable though.
I can see data being added (after I save) using MapTableAdapter.Insert. Is this fine to use or is there a more elegant and/or proper way to populate the table? (i.e., does this work, or should I be looking toward what you describe above?)
Thanks,
Ken
Re: Populating a Table programmatically
Once the DataTable ha sbeen populated you should be calling Update on the TableAdapter and saving the whole table in one go.