Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ACD DataTable events won't trigger custom aggregate calculation

Status
Not open for further replies.

dalchri

Programmer
Apr 19, 2002
608
US
I have a wholesale base total that is calculated off of a specifications table. This aggregate is too complicated for the Expression property of a DataColumn because it involves rounding and excluding part of the DataTable.

Therefore, I am calculating it myself as the following DataTable events occur: ColumnChanged (only if it is the total column), RowChanged (only if it is an DataRowAction.Add change), and RowDeleted. I am using the following code:

Code:
tblSpecification.Compute("SUM(Total)", "fkCategory <> " + this.BuilderSiteCosts);

The problem is that the RowChanged event occurs BEFORE the new row is actually added to the DataTable so the result of the Compute method is less the added row.

All other events work fine. RowDeleted occurs after the row is actually removed from the DataTable, ColumnChanged occurs after the column values are actually changed.

My workaround is to add the value of the added row onto the result of Compute only for that event.

I've also tried using an aggregate column such as:
Code:
new DataColumn("BaseTotal", typeof(double), "SUM(Total)")
Even though the result does not produce what I need, I was hoping that the DataTable would provide proper notification of a change to that column in ColumnChanged at which point I could trigger my custom calculation. There is no event notification this way either.

Does anyone know of a better way than my workaround?
 
Here could be a solution:
object objTotal;
// Assume the DataTable has a column named "Total."
// add a ColumnChanged event handler for the table.
myTable.ColumnChanged += new DataColumnChangeEventHandler( Column_Changed );
private static void Column_Changed( object sender, DataColumnChangeEventArgs e )
{
if (e.Column.ColumnName != "Total")
{
return;
}
objTotal = Calculate();
}

private void Calculate(DataTable dt)
{

myTable.Compute("Sum(Total)", "fkCategory <> 1000");

}
-obislavu-
 
obislavu,

Thank you for the post. The problem I have though is with the Add event notification. ColumnChanged does not fire when you add a row (which potentially affects my aggregate). I capture the RowChanged event as follows:
Code:
private void tblSpecificationRowChanged(object sender, tblSpecificationRowChangeEvent e) {
if (e.Action == DataRowAction.Add) {
objTotal = tblSpecification.Compute("SUM(Total)", "fkCategory <> " + this.BuilderSiteCosts);
}
}

Let's say I start with one row with Total = $100 and I add a second row with Total = $100. I would think that Compute should return $200 but it doesn't. It still returns $100.

When I inspect DataTable.Rows.Count, it returns 1, not 2 indicating that the new row hasn't actually been added yet.

My workaround is the following:
Code:
private void tblSpecificationRowChanged(object sender, tblSpecificationRowChangeEvent e) {
if (e.Action == DataRowAction.Add) {
objTotal = tblSpecification.Compute("SUM(Total)", "fkCategory <> " + this.BuilderSiteCosts) + [b]e.Row.Total[/b];
}
}

I do have a solution but I'm just wondering if I missed something or if there is an easier way than trapping the 3 ACD events.

BTW, yes it is the RowChanged event I am trapping, NOT the RowChanging event.
 
I did the followings:
public DataTable custTable;
object objTotal;
private void StartWatchDataTableRowChanged()
{
custTable.AcceptChanges();
custTable.RowChanged += new DataRowChangeEventHandler( Row_Changed );
}
private void Row_Changed( object sender, DataRowChangeEventArgs e )
{
objTotal = custTable.Compute("sum(Total)","fkCategory >0");
this.textBox1.Text = string.Format("{0}",objTotal);
}
private void btnStartWatch_Click(object sender, System.EventArgs e)
{
StartWatchDataTableRowChanged();
}

// Everywhere when the custTable is changed e.g. add row, delete row CALL AcceptChanges()
custTable.Rows.Add( ...);
custTable.AcceptChanges();

You should see the textbox updated with the right value of the sum;
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top