zoukankan      html  css  js  c++  java
  • 將一個有層級關級的數據表按父子關係進行排序整理并返回

    我們在很多時候,都要顯示TreeView或TreeGrid. 像jqGrid這種控件可以顯示TreeGrid.
    但是jqGrid要想顯示正確,必須在傳遞給它數據之前,要將數據集按父子關係排序好。即:

    ID|ParenetID|value
    -------------------
    1 |0        |....
    2 |1        |....
    3 |2        |....
    4 |1        |....

     1 private void button1_Click(object sender, EventArgs e)
     2         {
     3             DataSet ds = new DataSet();
     4 
     5             DataTable dt = new DataTable();
     6 
     7             dt.Columns.AddRange(new DataColumn[] 
     8             {
     9                 new DataColumn("SysID"),
    10                 new DataColumn("ParentSysID") ,
    11                 new DataColumn("SomeValue") ,
    12                 new DataColumn ("depth"),
    13                 new DataColumn ("seqNo",typeof(int))
    14             }) ;
    15 
    16             DataRow dr = dt.NewRow();
    17             dr[0] = "1";
    18             dr[1] = "0";
    19             dr[2] = "value";
    20             dr[3] = "0";
    21             dr[4] = 1;
    22             dt.Rows.Add(dr);
    23 
    24             dr = dt.NewRow();
    25             dr[0] = "2";
    26             dr[1] = "1";
    27             dr[2] = "value";
    28             dr[3] = "1";
    29             dr[4] = 3;
    30             dt.Rows.Add(dr);
    31 
    32             dr = dt.NewRow();
    33             dr[0] = "3";
    34             dr[1] = "1";
    35             dr[2] = "value";
    36             dr[3] = "1";
    37             dr[4] = 2;
    38             dt.Rows.Add(dr);
    39 
    40             dr = dt.NewRow();
    41             dr[0] = "4";
    42             dr[1] = "2";
    43             dr[2] = "value";
    44             dr[3] = "2";
    45             dr[4] = 1;
    46             dt.Rows.Add(dr);
    47 
    48             dr = dt.NewRow();
    49             dr[0] = "5";
    50             dr[1] = "0";
    51             dr[2] = "value";
    52             dr[3] = "0";
    53             dr[4] = 10;
    54             dt.Rows.Add(dr);
    55 
    56             dr = dt.NewRow();
    57             dr[0] = "6";
    58             dr[1] = "0";
    59             dr[2] = "value";
    60             dr[3] = "0";
    61             dr[4] = 5;
    62             dt.Rows.Add(dr);
    63 
    64             dr = dt.NewRow();
    65             dr[0] = "7";
    66             dr[1] = "5";
    67             dr[2] = "value";
    68             dr[3] = "1";
    69             dr[4] = 1;
    70             dt.Rows.Add(dr);
    71 
    72             DataTable dt1 = dt.Clone();
    73             var d = dt.AsEnumerable().Min(m => m.Field<string>("depth"));
    74             var a = dt.AsEnumerable().Select(m => { return new {sysid = m.Field<string>("sysID"),seqno = m.Field<int>("seqNo"), depth = m.Field<string>("depth") }; }).OrderBy(k => k.depth);
    75             var c = a.First().depth;
    76             var k1 = a.First().sysid;
    77 
    78             RecrusionLook<string,string>(dt, dt1, d, default(string));
    79 
    80         }
    81 
    82         private void RecrusionLook<T,K>(DataTable source, DataTable target, T depth, K parentSysID)
    83         {
    84             var result = from r in source.AsEnumerable()
    85                          where (r.Field<T>("depth").Equals(depth) || depth == null || depth.Equals(default(T))) && (r.Field<K>("ParentSysID").Equals(parentSysID) || parentSysID == null  || parentSysID.Equals(default(K)))
    86                          orderby r.Field<int>("seqNo")
    87                          select r;
    88 
    89             //result.Select(i => { target.ImportRow(i); RecrusionLook(source, target, "", i.Field<string>("sysID")); return; });
    90             foreach (var row in result)
    91             {
    92                 target.ImportRow(row);
    93                 RecrusionLook<T, K>(source, target, default(T), row.Field<K>("sysID"));
    94             }
    95 
    96         }
    View Code


     

  • 相关阅读:
    使用Mxnet基于skip-gram模型实现word2vect
    【快学springboot】SpringBoot整合Mybatis Plus
    面试官:说说Spring中的事务传播行为
    「快学SpringBoot」配置文件的加载顺序和配置项默认值设置
    「快学springboot」SpringBoot整合freeMark模板引擎
    「快学springboot」SpringBoot多环境配置文件
    为什么阿里规约手册要求谨慎使用Arrays.asList方法
    「快学Docker」Docker简介、安装和Hello World实现
    Java中的transient关键字
    IDEA设置窗口标签换行显示
  • 原文地址:https://www.cnblogs.com/sdikerdong/p/3359346.html
Copyright © 2011-2022 走看看