Sumit Deshpande
Sumit Deshpande

Reputation: 2155

C#.NET: How to create self data relationship on single DataTable?

I've following data table structure -

User ID | User Name | Manager ID
-------------------------------------
1       | ABD       | 2
2       | BCD       | NULL
3       | KUM       | 4
4       | POC       | NULL
5       | OJM       | 2

In the above table User ID - 2 is the manager of User ID - 1,5 similarly User ID - 4 is the manager of User Id - 3.

How to create self relationship to show hierarchical details of Manager -> Users?

Upvotes: 4

Views: 2343

Answers (1)

Parag Meshram
Parag Meshram

Reputation: 8511

You have to add nested relation for DataTable as below example -

DataRelation relation = new DataRelation("ParentChild",
        result.Tables["Employee"].Columns["UserID"],
        result.Tables["Employee"].Columns["ManagerID"],
        true);

relation.Nested = true;
result.Relations.Add(relation);

Hope this helps.

Upvotes: 3

Related Questions