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

标签 c# .net ado.net

我有以下数据表结构-

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

在上表中,User ID - 2 是 User ID - 1,5 的管理员,类似地,User ID - 4 是 User Id - 3 的管理员。

如何创建 self 关系以显示经理 -> 用户的层次细节?

最佳答案

您必须为 DataTable 添加嵌套关系,如下例 -

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

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

希望这对您有所帮助。

关于C#.NET : How to create self data relationship on single DataTable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12595612/

相关文章:

c# - 一对零或一关系,其中 FK 是从属表上的复合 PK 的一部分

javascript - 如何从 JavaScript 调用通用 WebAPI 操作?

sql-server-2008 - 程序中表值参数中的标识列,如何定义DataTable

c# - 元数据异常 : Unable to load the specified metadata resource

c# - 只读数据库上的索引

c# - 将 Predicate<T> 转换为 Expression<Func<T, bool>>

.net - 限制 WebClient DownloadFile 最大文件大小

.net - 重定向时将数组或字符串列表从一个操作传递到另一个操作

c# - 覆盖 Picturbox OnPaint 事件以旋转图像 - 创建自定义 Picturebox

c# - 在mysql连接字符串中指定密码