I have a datagridview which is bound to a dataview. I realize that any new rows must be added to the source datatable for it to work. However, in terms of user experience, I would like the users to be able to add rows without an 'add new row' button. The user should be able to tab to or click the bottom most row and fill in the values, which in turn creates the new data row programmatically, using the values entered in the DataGridViewRow. I've tried doing this with the 'UserAddedRow' and 'RowLeave' events.
In my case, the DataTable has 4 columns, but only 2 of them are being exposed to the user ("Quantity", and "Modifier"). The problem I'm having is that the last cell, "Modifier" is DBNull rather than the value the user typed in. This results in an InvalidCastException. So I've resorted to using a cockamimi 'add new row' button.
Is there a way to do this? Is it too much to expect for the user to get the same experience from a control regardless of the way it was filled / maintains its data?
Here's my code:
'the boolean value shared by UserAddedRow event and RowLeave event
Private _row_added As Boolean
Private Sub DataGridView1_UserAddedRow(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.UserAddedRow
'let 'RowLeave' know that a new row has been added
_row_added = True
End Sub
Private Sub DataGridView1_RowLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
'if a new row was added
If _row_added Then
'create a new datarow
Dim _new_item_pricing_table_row = appdata.Dataset.Item_Pricing_Table.NewItem_Pricing_TableRow()
'some default values that satisfy our current DataView RowFilter
_new_item_pricing_table_row.PID = _pid
_new_item_pricing_table_row.Pricing_Var_ID = _filter
'copy the values the user just typed in...
'this is the part that doesn't work.
With DataGridView1.Rows(e.RowIndex)
_new_item_pricing_table_row.Quantity = .Cells("Quantity").Value
'
_new_item_pricing_table_row.Modifier = .Cells("Modifier").Value '<--- the InvalidCastException occurs
'
End With
appdata.Dataset.Item_Pricing_Table.Rows.Add(_new_item_pricing_table_row)
'reset the _row_added flag
_row_added = False
End If
End Sub
Thanks for any help,
GHed