Computed column not updating when deleting a row from child DataTable

M

Mike Kiefer

I have a DataSet with two DataTables: Stores and CartItems. There is a data
relation called "StoreCartItem" that makes the CartItems table a child of
the Stores table. In the CartItems table I have a simple computed column
called ExtendedPrice with an expression of "PerItemPrice*Quantity". In the
Stores table I have a computed column called "Subtotal" with the expression:
"Sum(Child(StoreCartItem).ExtendedPrice)"

For the most part, everything works fine. When I add a new CartItem or
change the quantity for an existing CartItem, the Subtotal column of the
parent table is updated properly. However, the Subtotal column is not
updated in the corresponding parent row if I delete a CartItem using the
DataRow.Delete() method. I have managed to work around this problem by first
setting Quantity=0 and then immediately deleting the CartItem. It seems like
a bug that I must do it this way. Has anyone else experienced this? Is there
something else I should be looking for?

Thanks in advance,
Mike
 
K

Kevin Yu [MSFT]

Hi Mike,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when a row is removed from child table,
the value of the computed column is not updating. If there is any
misunderstanding, please feel free to let me know.

Based on my research, this is a known issue in current version of .NET
Framework. The column with expressions will not recompute value when child
or parent rows are deleted. Here I have a workaround. You can handle the
DataTable.RowDeleted event to re-set the expression for the subtotal column
with the following 2 line of code.

dataSet.Tables["Stores"].Columns["Subtotal"].Expression = "";
dataSet.Tables["Stores"].Columns["Subtotal"].Expression =
"Sum(Child(StoreCartItem).ExtendedPrice)";

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
M

Mike Kiefer

Yes, your understanding is correct. I'm actually glad to hear that this is a
bug and that I'm not going crazy...

Thanks for your help, and I like the way the workaround functions.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top