JI
jim
February 2, 2003 10:43 PM UTC
There may be a better way, but here are two ways to do what you suggested: Set the "allow db null" property to true, and set it after the row is added.
You can go after the "Table Row Changed" event on the data source. If the key field(s) are null, set them:
AddHandler dtMaster.RowChanged, AddressOf HandleMasterTableRowChanged
Protected Overrides Sub HandleMasterTableRowChanged(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs)
'ID goes in as null, but be "saved" as numassign
If e.Row("employee_id") Is DBNull.Value Then
Dim rowKey As Int32 = GetNewKey()
"employee_id"= rowKey
End If
End Sub
******
The other way is trap the the ListChanged event of the datasource. This is a really usefull event because it fires when the grid is sorted, or when you add or delete a row.
AddHandler dtMaster.DefaultView.ListChanged, AddressOf HandleMasterViewListChange
Protected Overrides Sub HandleMasterViewListChange(ByVal sender As Object, ByVal args As System.ComponentModel.ListChangedEventArgs)
If args.ListChangedType = System.ComponentModel.ListChangedType.ItemAdded Then
Dim dt As DataTable
dt = DataManager.DataTable
'You get an Added when you move to the new row, and when you save it.
'Ignore the case of moving to the row... there's not row in the datatable yet!
Dim dv As DataView
dv = CType(sender, DataView)
If dt.Rows.Count = args.NewIndex Then
Dim oCurrent As Object
oCurrent = m_bindingmanager.Current
Dim drv As DataRowView = CType(oCurrent, DataRowView)
Dim dr As DataRow = drv.Row
If dr.RowState = System.Data.DataRowState.Added Then
If dr("employee_id") Is DBNull.Value Then
Dim rowKey As Int32 = GetNewKey()
dr("employee_id") = rowKey
End If
End If
End If
End If
End Sub