tags:

views:

26

answers:

1

I have three columns in my data table.

1) ProductID
2) ProductParentID
3) ProductTotal

ProductID and ProductParentID are self refrencing columns where i can set parent child relationship and get child rows based on my relationship. Let us say i have following data
Product1
    Product11
    Product12
    Product13
        Product131
        Product132
        Product133
Product2
    Product21
    Product22
    Product23

Next to above hierarchy in Product total column what i want is total of each child rows and sum of those child rows product total should be rolled up to it parent product. E.g if Product 131 total is 10,Product 13 total is 15 and Product 133 total is 5 then the product 13 total should be 30. The logic should work for n number of self hierarchy.

Is there any functionality in data table itself where i can achieve this without iterating through each row and do it manually ?

Thanks.

+1  A: 

Have you looked at the DataRelation? In your case:-

        DataRelation productRelation= new DataRelation("ProductRelation",
        oDs.Tables["Product"].Columns["ProductId"],
        oDs.Tables["Product"].Columns["ProductParentId"]);
        dataSetObj.Relations.Add(productRelation);

and then use LINQ to DataSet to achieve something like described here in SO.

ydobonmai
Thank you.Yes, i am already using DataRelation and was looking for something where i dont have to write much code. I am not using linq so for now i achieved my requirement by writing some recursive code to get total of child rows and setting it to its parent row field. Btw, had i been using Linq can we achieve it for N level of hierarchy ? as in my case i can have any number fo child tables.
Nikhil Vaghela