Hi Kevin
Thanks for the reply.
I have a Form1 containing a ReadOnly sortable DataGrid and Add, Modify and
Delete buttons. I also have a strongly typed dataset containing a master
tAddressBook table and a child tAddressBookContact table, both with
autoincrement = True, autoincrementseed & step = -1. A one-many Relationship
exists with UpdateRule and DeleteRule as cascade.
The selected row in the datagrid is obtained by:
-----------------------------------------------------------------------------
Dim bm As BindingManagerBase =
Me.dgAddressBook.BindingContext(Me.dgAddressBook.DataSource,
Me.dgAddressBook.DataMember)
Dim dr As DataRow = CType(bm.Current, DataRowView).Row
intpkID = dr.item("AddressBookID")
-----------------------------------------------------------------------------
Clicking the Delete simply deletes the selected record (except there is a
bug in the datagrid for deleting last row for ReadOnly datagrid, but that's
another post!).
The Modify button launches a Form2. I give Form2 a new property DataSource
and set it to the Dataset, and also a new property AddressBookID and set it
to the intpkID above. Form2 then needs to find this again (because the order
of the datatable, does not match the sorted grid) and does so successfully
whether the datagrid is sorted or not using:
-----------------------------------------------------------------------------
Me.mcmAddressBook = Me.BindingContext(Me.DataSource, "tAddressBook")
Me.DataSource.tAddressBook.DefaultView.Sort = "AddressBookID ASC"
Me.mcmAddressBook.Position =
Me.DataSource.tAddressBook.DefaultView.Find(Me.AddressBookID)
-----------------------------------------------------------------------------
where mcmAddressBook is a form level CurrencyManager.
The Add button first adds a new strongly typed datarow to the datatable:
-----------------------------------------------------------------------------
Dim drAddressBook As dsAddressBook.tAddressBookRow
drAddressBook = Me.DsAddressBook1.tAddressBook.NewRow()
Me.DsAddressBook1.tAddressBook.Rows.Add(drAddressBook)
intpkID = drAddressBook.item("AddressBookID")
-----------------------------------------------------------------------------
It then launches a Form2. I then pass the Dataset and AddressBookID as with
the Modify. Form2 then needs to find the AddressBookID again using:
-----------------------------------------------------------------------------
Me.mcmAddressBook = Me.BindingContext(Me.DataSource, "tAddressBook")
Me.DataSource.tAddressBook.DefaultView.Sort = "AddressBookID ASC"
Me.mcmAddressBook.Position =
Me.DataSource.tAddressBook.DefaultView.Find(Me.AddressBookID)
-----------------------------------------------------------------------------
This finds it in the first position because the autoincrement id of the
newly added row is -1 (all existing ids are 0 or positive). The actual row
exists in the last position of the datatable.
The DataSource of all controls in Form2 is the dataset passed in, i.e.
Me.DataSource.
The DataMember of all controls is a string comprising of the
DataTable.ColumnName.
There also exists on Form2 another datagrid that has the DataMember of
DataTable.Relation_AddressBook_AddressBookContact.
Maybe I need to bind to a DataView or something, but this all seems so much
more complicated than it needs to be.
At the moment, for the Add condition, as a work around I use:
-----------------------------------------------------------------------------
Me.mcmAddressBook.Position = Me.DataSource.tAddressBook.Rows.Count - 1
-----------------------------------------------------------------------------
which works but is extremly ropey.
BACKGROUND:
From other posts you have suggested passing in the DataRow and using
GetChildRows to populate the Form2 datagrid. This need the manual filling of
another table to bind to the grid for it to work correctly (i.e. not show
'HasRows' etc. columns), and because I will have many hierarchical forms in
the above format, I consider that this will be too much additional coding for
the simple operations that I want to conduct. I am therefore passing in the
Dataset and going from there.
Many Thanks