My assumption is that you are using a bindingsource with the datagridview? Add the dataset to the datasource for your project (Menu: Data->Add New Data Source). Then Menu: Data->Show Data Sources. Right click on the table and select to show it as a datagrid. Drag the table onto your form. Right click on the grid to Edit Columns. Delete the columns you don't want, format the rest. The designer will automatically create a bindingsource for you. In your form code, select the Bindsource on the top left pulldown list and then 'AddingNew' on the top right list. A new event will be created for you. Inside the event, add the following code:
Private Sub YourTableBindingSource_AddingNew(ByVal sender As Object, _
ByVal e As system.ComponentModel.AddingNewEventArgs) _
Handles bsyourBinding.AddingNew
Dim bs As BindingSource = CType(sender, BindingSource)
Dim view As System.Data.DataView = CType(bs.List, System.Data.DataView)
Dim _row As System.Data.DataRowView
_row = view.AddNew
dim newRow as yourDataSet.yourTableRow = DirectCast(_row.Row, yourDataSet.yourTableRow)
With newRow
.BeginEdit()
NextIDNumber += 1
.MyIDField = NextIDNumber
.MyDataField1 = Today.AddDays(1).Date
.MyDataField2 = someData
.MyDataField3 = someOtherData
.EndEdit()
End With
bs.MoveLast()
e.NewObject = _row
End Sub
This binding source AddingNew event is particularly useful when you are adding records that belong to a parent table. As the tableis bound to the grid using a bindingsource, all changes made to the bindingsource are automatically reflected in the grid. You don't have to display the ID Number in the grid. If a user clicks on a new line on the grid, this binding source event will be automatically fired.
I would also include some code for the grid events CellParsing, CellValidating, CellValidated (use this to clear out error messages and maybe do some math) and RowValidating. In Row Validating you can check that all of the cells have values and are valid. If the user has left a blank line, you can call .CancelEdit to delete the blank line and prevent it from getting stored to the database.