Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / June 2004

Tip: Looking for answers? Try searching our database.

Tree & Sum of Nodes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James E - 05 May 2004 08:50 GMT
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
>>>Node 2 (Parent Is Node 1) - Value 100.00
>>>Node 3 (Parent Is Node 1) - Value 150.00
Node 4 (Root Node) - Value 250.00
>>>Node 5 (Parent Is Node 4) - Value 75.00
>>>>>>Node 6 (Parent Is Node 5) - Value 375.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)

>>>Node 2 (Parent Is Node 1) - Value 100.00 NewVal 100.00 (It's own value
only)

>>>Node 3 (Parent Is Node 1) - Value 150.00 NewVal 150.00 (It's own value
only)

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

>>>Node 5 (Parent Is Node 4) - Value 75.00 NewVal 450.00 (75.00 + 375.00)

>>>>>>Node 6 (Parent Is Node 5) - Value 375.00 NewVal 375.00 (It's own value
only)

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

Cheers

James E
Kevin Yu [MSFT] - 06 May 2004 06:38 GMT
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
Signature

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

Kevin Yu [MSFT] - 11 May 2004 06:30 GMT
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
Signature

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

James E - 02 Jun 2004 16:25 GMT
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

> Hi James,
>
[quoted text clipped - 6 lines]
> "This posting is provided "AS IS" with no warranties, and confers no
> rights."

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.