Here it is.
If you change a value in the child page, it is reflected on the parent.
If you select a child row and hit the delete key, the parent still shows
the same value (still sees the deleted row). If you then change a value
on another row of the child, the parent is now calculating correctly and
no longer sees the deleted row.
Thanks for looking at it.
namespace test
{
using System;
using System.IO;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
public class MyApp : System.Windows.Forms.Form
{
private System.Windows.Forms.TabControl tabControl;
private System.Windows.Forms.TabPage parentPage, childPage;
private System.Windows.Forms.DataGrid parentDataGrid,
childDataGrid;
private DataSet ds;
private Button acceptButton;
public static void Main()
{
Application.Run(new MyApp());
}
public MyApp()
{
//
// Data Sets
//
this.ds = new DataSet();
CreateDS(ds);
//
// Parent Page
//
this.parentPage = new TabPage("Parent");
this.parentPage.Size = new System.Drawing.Size(456,256);
this.parentDataGrid = new System.Windows.Forms.DataGrid();
parentDataGrid.BeginInit();
parentDataGrid.CaptionVisible = false;
parentDataGrid.Location = new System.Drawing.Point(2,2);
parentDataGrid.Size = new System.Drawing.Size(452,252);
parentDataGrid.TabIndex = 1;
parentDataGrid.Anchor = AnchorStyles.Top | AnchorStyles.Left |
AnchorStyles.Right | AnchorStyles.Bottom;
parentDataGrid.DataSource = ds.Tables["Parent"].DefaultView;
((DataView)parentDataGrid.DataSource).AllowNew = false;
((DataView)parentDataGrid.DataSource).AllowDelete = false;
parentDataGrid.AllowNavigation = false;
parentDataGrid.EndInit();
this.parentPage.Controls.Add(this.parentDataGrid);
//
// Child Page
//
this.childPage = new TabPage("Child");
this.childPage.Size = new System.Drawing.Size(456,256);
this.childDataGrid = new System.Windows.Forms.DataGrid();
childDataGrid.BeginInit();
childDataGrid.CaptionVisible = false;
childDataGrid.Location = new System.Drawing.Point(2,42);
childDataGrid.Size = new System.Drawing.Size(452,212);
childDataGrid.TabIndex = 1;
childDataGrid.Anchor = AnchorStyles.Top | AnchorStyles.Left |
AnchorStyles.Right | AnchorStyles.Bottom;
childDataGrid.DataSource = ds.Tables["Child"].DefaultView;
((DataView)childDataGrid.DataSource).AllowNew = false;
//((DataView)childDataGrid.DataSource).AllowDelete = false;
childDataGrid.AllowNavigation= false;
childDataGrid.EndInit();
this.childPage.Controls.Add(this.childDataGrid);
//
// Tab Control
//
this.tabControl = new System.Windows.Forms.TabControl();
this.tabControl.Location = new System.Drawing.Point(2,2);
this.tabControl.Name = "Tab";
this.tabControl.SelectedIndex = 0;
this.tabControl.Size = new System.Drawing.Size(460,260);
this.tabControl.TabIndex = 0;
this.tabControl.Anchor = AnchorStyles.Top| AnchorStyles.Left |
AnchorStyles.Bottom | AnchorStyles.Right;
tabControl.TabPages.Add( new TabPage("X") ); // fix for layout
problems on first page - temp page
tabControl.TabPages.Add(parentPage);
tabControl.TabPages.Add(childPage);
//
// Accept Button
//
acceptButton = new Button();
acceptButton.Location = new System.Drawing.Point(120,10);
acceptButton.Size = new System.Drawing.Size(100,24);
acceptButton.Text = "AcceptChanges";
acceptButton.Click += new EventHandler( this.Accept_Click );
this.childPage.Controls.Add(this.acceptButton);
//
// Form
//
this.AutoScaleBaseSize = new System.Drawing.Size(5,13);
this.ClientSize = new System.Drawing.Size(464,264);
this.Text = "test";
this.Controls.Add(this.tabControl);
}
private void Accept_Click(object sender, EventArgs e)
{
ds.AcceptChanges();
}
public static void CreateDS(DataSet ds)
{
DataTable parent = new DataTable("Parent");
parent.Columns.Add(new DataColumn("ID", typeof(string)));
parent.Columns.Add(new DataColumn("Assigned", typeof(decimal)));
parent.PrimaryKey = new DataColumn[] { parent.Columns["ID"] };
ds.Tables.Add(parent);
DataTable child = new DataTable("Child");
child.Columns.Add(new DataColumn("ID", typeof(string)));
child.Columns.Add(new DataColumn("Category", typeof(string)));
child.Columns.Add(new DataColumn("Amount", typeof(decimal)));
child.PrimaryKey = new DataColumn[] { child.Columns["ID"],
child.Columns["Category"]
};
ds.Tables.Add(child);
ds.Relations.Add("ParentChild",
new DataColumn[1]{parent.Columns["ID"]},
new DataColumn[1]{child.Columns["ID"]});
parent.Columns["Assigned"].Expression =
"ISNULL(Sum(Child(ParentChild).Amount), 0)";
DataRow r = parent.NewRow();
r["ID"] = "A";
parent.Rows.Add(r);
for( Decimal d = 1; d< 10; d++ )
{
r = child.NewRow();
r["ID"] = "A";
r["Category"] = "B-" + d.ToString();
r["Amount"] = d;
child.Rows.Add(r);
}
ds.AcceptChanges();
}
}
}
*** Sent via Developersdex
http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!