开发者

how to get group total in self refrenced data in data table?

I have three columns in my data table.

1) ProductID

2) ProductParentID

3) P开发者_C百科roductTotal

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜