Tree & Sum of Nodes

  • Thread starter Thread starter James E
  • Start date Start date
J

James E

I have a weird situation, working with some data from an old unix system.
There is a tree structure, where all nodes (i.e. non-leaf nodes) can have
values, as well as the leaf nodes.

Node 1 (Root Node) - Value 50.00
I have loaded the data into a dataset, with a single datatable, parent-child
relationship and a Primary Key column. Once the data is loaded, I have added
an expression column with the expression:

Sum(child(myRelationName).Value)

this works ok up to a point, giving me a column that displays the sum value
of each nodes children, but what I am after is this. I want to have a column
that displays, at each node level, the sum of the nodes value plus the sum
of the children and those childrens children, etc, etc.

In my example above, the values would be as follows (given a new column name
'NewVal'):

Node 1 (Root Node) - Value 50.00 NewVal 300.00 (50.00 + 100.00 + 150.00)
only)

Node 4 (Root Node) - Value 250.00 NewVal 700.00 (250.00 + 75.00 + 375.00)
only)


ok...hope I have explained that alright. Can anyone help me with this
one...please!

Cheers

James E
 
Hi James,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to sum all values of a node
and all nodes under it. If there is any misunderstanding, please feel free
to let me know.

As far as I know, the Expression property of a DataColumn object doesn't
support recursive summary. So I think we have to go through all the nodes
under the root node to summary values. Here I have written a code snippet
that get the summary of a node and it's child values recursively. HTH.

Public Function summary(ByVal dr As DataRow) As Single
Dim ret As Single
ret = dr.Value
Dim childdr As DataRow()
childdr = dr.GetChildRows("myRelationName")
If childdr.Length > 0 Then
For Each child As DataRow In childdr
ret = ret + summary(child)
Next
End If
Return ret
End Function

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

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Kevin,
my most sincere apologies for not replying and thanking you sooner. Your
code was a great help and with a very minor change I was able to get exactly
what I wanted. My C# code I ended up using was as follows:

protected void LoadData()
{
//some code to load dataset, add primary key to ID column,
//add datarelation between ID and ParentID columns


ds.Tables["MyTable"].Columns.Add("cumulative",
typeof(System.Decimal));

DataView dv = new DataView(ds.Tables["MyTable"]);

//Set the row filter to be root nodes in the tree
dv.RowFilter = "ParentID IS NULL";
dv.Sort = "ID asc";

foreach(DataRowView dr in dv)
{
dr["cumulative"] = Summary(dr.Row);
}

this.ultraGrid1.DataSource = dv;
this.ultraGrid1.Refresh();

}


protected decimal Summary(DataRow dr)
{

decimal ret = 0;
ret = (dr["cost"] == System.DBNull.Value) ? 0 :
Convert.ToDecimal(dr["cost"].ToString());
DataRow[] childRows = dr.GetChildRows("myRelationName");

decimal childTotal = 0;

foreach(DataRow child in childRows)
{
childTotal = Summary(child);
child["cumulative"] = childTotal;
ret += childTotal;
}

return ret;
}



the dataview was bound to an Infragistics UltraGrid which allowed me to
display the data in a tree-like datagrid. Thank you again for your
help....it was most helpful!!!

Regards

James
 
Back
Top