473,569 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

datatable relations with column expression bug

I have 2 tables that have a relation between them. The parent table
also has an expression in a column that sums the values in the child
table. Both tables are hooked up to datagrids.

Everything seems to work fine as far as adding and changing rows in the
child table. But if I delete a row in the child table that has a value
in it, the parent table continues to have a total that includes the
deleted child row... even after calling accpetChanges on the dataset.

I can scan through the rows in the child table and sure enough, there
are no deleted rows because acceptChanges removed it. The datagrid for
the child table doesn't show the row either... just the parent seems to
be keeping track of the phantom row.

If I change a value in another row in the child table and do another
acceptChanges, then finally the parent stops adding the deleted row to
its calculation.

When I am deleting the row, I am doing so from the datagrid by selecting
the row and hitting the delete key... haven't checked if I get the same
problem by deleting the row programatically .

Any ideas as to what is going on?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
3 4013
Hi Edward
There must be some event that was fired only on the case of modification
and not when a row was deleted( from the grid UI) . would you mind posting
an appropriate snippet of your code

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #2
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.TabContro l 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.parentData Grid = 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.To p | AnchorStyles.Le ft |
AnchorStyles.Ri ght | AnchorStyles.Bo ttom;
parentDataGrid. DataSource = ds.Tables["Parent"].DefaultView;
((DataView)pare ntDataGrid.Data Source).AllowNe w = false;
((DataView)pare ntDataGrid.Data Source).AllowDe lete = false;
parentDataGrid. AllowNavigation = false;
parentDataGrid. EndInit();
this.parentPage .Controls.Add(t his.parentDataG rid);

//
// Child Page
//
this.childPage = new TabPage("Child" );
this.childPage. Size = new System.Drawing. Size(456,256);
this.childDataG rid = new System.Windows. Forms.DataGrid( );
childDataGrid.B eginInit();
childDataGrid.C aptionVisible = false;
childDataGrid.L ocation = new System.Drawing. Point(2,42);
childDataGrid.S ize = new System.Drawing. Size(452,212);
childDataGrid.T abIndex = 1;
childDataGrid.A nchor = AnchorStyles.To p | AnchorStyles.Le ft |
AnchorStyles.Ri ght | AnchorStyles.Bo ttom;
childDataGrid.D ataSource = ds.Tables["Child"].DefaultView;
((DataView)chil dDataGrid.DataS ource).AllowNew = false;
//((DataView)chil dDataGrid.DataS ource).AllowDel ete = false;
childDataGrid.A llowNavigation= false;
childDataGrid.E ndInit();
this.childPage. Controls.Add(th is.childDataGri d);

//
// Tab Control
//
this.tabControl = new System.Windows. Forms.TabContro l();
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.To p| AnchorStyles.Le ft |
AnchorStyles.Bo ttom | AnchorStyles.Ri ght;
tabControl.TabP ages.Add( new TabPage("X") ); // fix for layout
problems on first page - temp page
tabControl.TabP ages.Add(parent Page);
tabControl.TabP ages.Add(childP age);

//
// Accept Button
//
acceptButton = new Button();
acceptButton.Lo cation = new System.Drawing. Point(120,10);
acceptButton.Si ze = new System.Drawing. Size(100,24);
acceptButton.Te xt = "AcceptChanges" ;
acceptButton.Cl ick += new EventHandler( this.Accept_Cli ck );
this.childPage. Controls.Add(th is.acceptButton );

//
// Form
//
this.AutoScaleB aseSize = new System.Drawing. Size(5,13);
this.ClientSize = new System.Drawing. Size(464,264);
this.Text = "test";
this.Controls.A dd(this.tabCont rol);

}
private void Accept_Click(ob ject sender, EventArgs e)
{
ds.AcceptChange s();
}
public static void CreateDS(DataSe t ds)
{
DataTable parent = new DataTable("Pare nt");
parent.Columns. Add(new DataColumn("ID" , typeof(string)) );
parent.Columns. Add(new DataColumn("Ass igned", typeof(decimal) ));
parent.PrimaryK ey = new DataColumn[] { parent.Columns["ID"] };
ds.Tables.Add(p arent);

DataTable child = new DataTable("Chil d");
child.Columns.A dd(new DataColumn("ID" , typeof(string)) );
child.Columns.A dd(new DataColumn("Cat egory", typeof(string)) );
child.Columns.A dd(new DataColumn("Amo unt", typeof(decimal) ));
child.PrimaryKe y = new DataColumn[] { child.Columns["ID"],
child.Columns["Category"]
};
ds.Tables.Add(c hild);
ds.Relations.Ad d("ParentChild" ,
new DataColumn[1]{parent.Columns["ID"]},
new DataColumn[1]{child.Columns["ID"]});
parent.Columns["Assigned"].Expression =
"ISNULL(Sum(Chi ld(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.AcceptChange s();

}
}

}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3

I have found a way to work around this. Before accepting changes, clear
out the expression on the parent table. Accept changes. Put the
expression back on the parent table. Accept changes. Kind of kludgy;
but it works around the bug.
Also, another bug related to adding rows with a parent expression.
Using the same code as before, comment out the line in the child
datagrid:
((DataView)chil dDataGrid.DataS ource).AllowNew = false;

this allows you to add new rows now to the child. If you add a new row,
the parent calculates correctly. But if you try to change a value on
any of the other rows on the child table, and error is thrown about no
Original data to access. I can add and change as many new rows as I
want; but I can't change an existing row. Once I acceptChanges() to the
dataset, everything is working fine again.

Ed

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1425
by: Coreymas | last post by:
Hello everyone Here is what I am trying to do I have a datatable with 7 columns (called Mon, Tue, Wed,...) representing the days of the week I want to create a calculated column called totals that totals the 7 days of the week columns I try to use the following code
3
14302
by: MrNobody | last post by:
I've read that the expression property for DataColumns is used to "Sets or retrieves the expresssion used to filter rows, calculate the values in a column, or create an aggregate column.". I have seen examples on how to filter a column, but how would I filter out an entire row depending on the value of a column? For example, if I wanted...
0
3131
by: Chris Ericoli | last post by:
Hi, I am working with an 'in session' ado dataset with an asp.net application. My dataset is comprised of two tables, one of which maintains a few calculated datacolumns. For some reason these datacolumns do not trigger their expression when other columns from which the expressions are derived are updated. Below is a basic example of what...
1
2012
by: cindy | last post by:
Get data into datatable, add to dataset dsSearch " Get data into datatable, add to dataset dsSearch Using In-Memory SQL Engine join the tables and select the filenames from the join, add to dataset dsSearch CODE ON http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3994&lngWId=10 using SQL connection get data from view...
1
2225
by: Geoff Jones | last post by:
Hi Is it possible to change the type of data a column holds in DataTable at runtime? For example, suppose that the table column originally holds Strings, can we, at runtime, change it to Integers? I'm also interested in changing the date/time that may be held in a column. That is, if the date and time are held in a column of DataTable,...
11
19699
by: Bruce D | last post by:
I have a DataTable that I need to merge the 'lastname' and 'firstname' fields to be displayed in a datagrid. I know I can use the SQL statement to merge the fields into one, but I want to know if it's possible to do through the DataTable, DataView or DataGrid that I have already created to display the data. This should be easy...right? ...
4
2335
by: Dennis | last post by:
I am trying to set up an Expression column in a DataTable for display in a DataGrid that subtract the column ("BirthDay") from todays date. I can't seem to get it right...is this possible? -- Dennis in Houston
9
4002
by: Anil Gupte | last post by:
After reading a tutorial and fiddling, I finally got this to work. I can now put two tables created with a DataTable class into a DataRelation. Phew! And it works! Dim tblSliceInfo As New DataTable("SliceInfo") Dim tblSliceRatings As New DataTable("SliceRatings") '.... All the adding datacolumns, datarows, etc. goes here.. ...
0
2182
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads CSV file without any issues only if all the fields are not null. If any field is blank, it moves the values to the left and displays the value under...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.