如何以分级结构显示非规范化的数据? [英] How do I display non-normalized data in a hierarchical structure?

查看:152
本文介绍了如何以分级结构显示非规范化的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我想在一个层次结构,这样来显示数据:

My issue is that I want to display data in a hierarchal structure as so:




    • 县委书记

      • 候选人1

      • 候选人2


      • 候选人1

      • 候选人2

      • 候选3

      但我检索这样的数据集:

      But I'm retrieving the dataset like this:

      Party | Office | Candidate
      --------------------------------------------
      Democrat | County Clerk | Candidate 1
      Democrat | County Clerk | Candidate 2
      Democrat | Magistrate | Candidate 1
      Democrat | Magistrate | Candidate 2
      Democrat | Magistrate | Candidate 3
      



      我计划使用嵌套的中继器,但我需要党的独特价值,然后不同的为了使党内办公室名称的值来做到这一点。

      I planned on using nested repeaters, but I need a distinct value of Party, and then distinct values of office name within that party in order to do it.

      是否有任何.NET函数轻松地做我尝试?会有显示比中继器以外的信息的更好的办法?

      Are there any .NET functions to easily do what I'm attempting to? Would there be a better way of displaying the information other than repeaters?

      在此先感谢!

      推荐答案

      如果您可以修改存储过程,我会建议在邻接表的形式设置的结果:

      If you can modify the stored procedure, I would recommend a result set in the form of an adjacency list:

      ID    Name          ParentID
      1     Democrat      NULL
      2     County Clerk  1
      3     Magistrate    1
      4     Candidate 1   2
      5     Candidate 2   2
      6     Candidate 1   3
      7     Candidate 2   3
      8     Candidate 3   3
      

      它减少数据量检索,并允许您对父子关系递归编程。你只需从根开始。这种方法避免了使用嵌套中继器通过直接构建在字符串中的StringBuilder字面的麻烦。

      It reduces the amount of data retrieved, and allows you to program recursively against the parent-child relationships. You simply start at the root. This approach avoids the hassle of using nested Repeaters by directly building the string literal in a StringBuilder.

      StringBuilder sb = new StringBuilder();
      DataTable dt = new DataTable();
      someDataAdapter.Fill(dt);
      
      // find the roots:
      DataRow[] roots = dt.Select("parentId is null");
      
      sb.Append("<ul>");
      foreach (DataRow child in roots)
      {
          WriteNode(child, sb);
      }
      sb.Append("</ul>");
      
      
      // recursively write out each node
      public static void WriteNode(DataRow row, StringBuilder sb) {
          sb.Append("<li>"); 
          sb.Append(row["Name"]);
      
          // find children rows...
          DataRow[] children = row.Table.Select("parentId = " + row["id"].ToString());
              if (children.Length > 0)
              {
                  sb.Append("<ul>");
                  foreach (DataRow child in children)
                  {
                      WriteNode(child, sb);
                  }
                  sb.Append("</ul>");
              }
      
              sb.Append("</li>");
          }
      

      这篇关于如何以分级结构显示非规范化的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆