Master/Child DataGrids: Child data is lost when creating new rows

Master/Child DataGrids: Child data is lost when creating new rows

Post by Johannes Brodwa » Sat, 12 Jul 2003 21:47:00



I am creating an application that basically uses two tables in a
master/child DataRelationship like Dino Esposito's article "Data
Binding Between Controls in Windows Forms"
(http://msdn.microsoft.com/msdnmag/issues/02/02/cutting/). The
application uses a DataRelation in a DataMember property, and I have
some snags getting it to work correctly.

The behavior when ADDING new rows has two problems that I can't
understand. It seems like the behavior of the data binding is not
correct. Here are my problems:

1. Relationship is not maintained when I create a new child row for a
newly created master row:
 * Create a new master row, fill in primary key
 * Without moving to another master row, create a new child row
 * The child row is created without the foreign key set, if it is not
explicitly set, the row will disappear if another master row is
selected, and the original master is reselected.

This behavior is different from when the master row already exists. In
that case, the child row's foreign key is set correctly. It also works
correctly if the master row has an autoincrement primary key.

Q: How can I ensure that the master row's primary key is set on the
child row when a creating a new child for a newly created master?

2. Newly created child row disappears when selecting a new master row:
 * Select an existing master row
 * Create and edit a new child row, but do not move away from the row
 * Select a different (or new) master row
 * When returning to the first master row, the child row is missing

It seems like the editing is not ended when a new master in a
master/child databinding relationship is selected.

Q: How can I ensure that the edit is finished before selecting a new
master in a master/child relationship.

See attached source for an example that displays this behavior.

Thanks,
 ~ Johannes Brodwall

--
using System;
using System.Data;
using System.Windows.Forms;

public class GridMasterDetail : Form {
        DataGrid masterGrid = new DataGrid();
        DataGrid childGrid = new DataGrid();

        DataSet ds = CreateTestData();

        public GridMasterDetail() {
                childGrid.Dock = DockStyle.Fill;
                childGrid.Parent = this;
                childGrid.DataSource = ds;
                childGrid.DataMember = "MasterTable.MasterToChild";

                masterGrid.Dock = DockStyle.Top;
                masterGrid.Parent = this;
                masterGrid.DataSource = ds;
                masterGrid.DataMember = "MasterTable";
        }

        private static DataSet CreateTestData() {
                DataSet ds = new DataSet();

                DataTable masterTable = ds.Tables.Add("MasterTable");
                DataColumn masterKey  = masterTable.Columns.Add("Id", typeof(int));
                masterTable.Columns.Add("Name", typeof(string));
                masterTable.PrimaryKey = new DataColumn[] { masterKey };

                DataTable childTable = ds.Tables.Add("ChildTable");
                DataColumn childKey = childTable.Columns.Add("Id", typeof(int));
                DataColumn childFK  = childTable.Columns.Add("MasterId",
typeof(int));
                childTable.Columns.Add("Text", typeof(string));
                childTable.PrimaryKey = new DataColumn[] { childKey };

                ds.Relations.Add("MasterToChild", masterKey, childFK);

                return ds;
        }

        [STAThread]
        public static void Main(string[] args) {
                Application.Run(new GridMasterDetail());
        }

Quote:}

 
 
 

1. Repost: Child rows in data grid not updating position in child table's CurrencyManager..MS??

I'm using the windows.forms.datagrid to display 2 related tables from a
dataset.  I've related the 2 tables using a dataset relation object and
bound the grid initially to the parent table.  As far as the grid display
goes, everything works as expected - you can drill into the parents and link
to each parent's child rows.

However, I also have some text boxes on the form bound to the same dataset
tables (for editing parents or children).  When parent rows are selected in
the datagrid, although the CurrencyManager's position for the parent table
is changed (and therefore the values in the parent's textboxes follow),
having linked to a parent's children, selecting a child row does not cause
the child table's currency manager to be re-positioned, resulting in the
children's bound textboxes not being populated (if I manually change the
position of the children's currency manager, they are).

Is this a bug or by design, and is there a workaround to get the most out of
this datagrid parent-child feature?
An obvious approach would be to re-position the child records' data manager
manually in the datagrid's navigate event, but there doesn't seem to be a
way to relate the 2 rows (and I've done that in the dataset's relation
object anyway!)

I'd appreciate it if someone could point me in the right direction with this
one or at least confirm it's a known bug....I can stop pulling my hair out
then!

TIA,

--James, UK

2. Sneaky @Home internal Memo! - BANDWIDTH CAPS

3. Reading Child Row from a Datagrid displaying Parent-Child Realtionship

4. Optimization application order

5. Problem with Master/Detail DataGrids: "Cannot create child list for field ...."

6. Question regarding applying policies to just one group

7. BUG: Parent-Child expressions with new child records

8. Amiga U.S. mailorder soft phone numbers

9. Maximized MDI child resizing on new MDI child

10. Can a DataView be used as a master for a master/child relationship?

11. Child Rows of Row in Datagrid

12. Typed Data Set, Master Child, Relationship Case Sensitivity

13. DataGrid and mixed child master columns?