你的位置:首页 > ASP.net教程

[ASP.net教程]线性列表转树节点


有如下列表数据:

0-PRODUCT
1-1012000178
2-1012610976
3-1016000022
3-1016010178
4-1061000015
5-1062000002
5-1062000002
6-1062000002
7-1062000002
8-1013502230
9-1061010180
10-1062000002
11-1062000002
4-1061010180
4-1062000002
4-1062000002
2-1013502229
3-1061010180
3-1062000002
3-1062000002
4-1061000015
5-1062000002
5-1062000002
6-1062000002
7-1062000002
8-1013502230
9-1061010180
10-1062000002
11-1062000002
2-1013502230
3-1061010180
3-1062000002
3-1062000002
2-1013502231
3-1061010180
3-1062000002
3-1062000002
2-1013502232
3-1061000059
3-1061010180
3-1062000013
3-1062000013
2-1013502233
3-1061000059
3-1061010180
3-1062000013
3-1062000013
2-1013515923
3-1013502234
4-1061000015
4-1061010180
4-1062000002
4-1062000002
3-1013502235
4-1061010180
4-1062000002
4-1062000002
2-1016010130
1-1012101332
2-1016506656
2-1016550476
2-1014060198
3-1016020078

 

 1 private void Debug0(List<LayerRelation> list) 2     { 3       int num = -1; 4       TreeNode nowNode = null; 5       TreeNode node = null; 6       treeView1.Nodes.Clear(); 7       for (int i = 0; i < list.Count; i++) 8       { 9         var model = list[i];10         node = new TreeNode(string.Format("{0}-{1}", model.LayerNum, model.MaterielCode));11         if (model.LayerNum == 0)12         {13           treeView1.Nodes.Add(node);14         }15         else if (model.LayerNum - 1 == num)16         {17           nowNode.Nodes.Add(node);18         }19         else if (model.LayerNum == num)20         {21           nowNode.Parent.Nodes.Add(node);22         }23         else if (model.LayerNum + 1 == num)24         {25           TreeNode parentNode = null;26           for (int n = 0; n < model.LayerNum; n++)27           {28             parentNode = parentNode == null ? nowNode.Parent : parentNode.Parent;29             if (parentNode.Level == model.LayerNum)30             {31               parentNode.Parent.Nodes.Add(node);32               num = node.Level;33               nowNode = node;34               parentNode = null;35               break;36             }37           }38 39           if (parentNode != null)40           {41             parentNode.Nodes.Add(node);42             num = node.Level;43             nowNode = node;44           }45         }46         else47         {48           TreeNode topNode = null;49           for (int n = 0; n <= (num - model.LayerNum); n++)50           {51             topNode = topNode == null ? nowNode.Parent : topNode.Parent;52           }53 54           if (topNode != null)55            {56              topNode.Nodes.Add(node);57             num = node.Level;58             nowNode = node;59            }60         }61 62         num = node.Level;63         nowNode = node;64       }65     }

View Code