简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM