日期:2014-05-17 浏览次数:20944 次
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { var data = @"01 现金 02 银行 02001 建行 02001001 建行南宁分行 02001002 建行田阳支行 02001003 建行百色分行 02002 中行 02002001 中行南宁分行 02002002 中行田阳支行 02002003 中行百色分行 02003 农行".Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries) .Select(x => new { id = x.Split(' ')[0], name = x.Split(' ')[1] }); foreach (var item in data.Where(x => x.id.Length == 2)) { TreeNode tn = new TreeNode() { Name = item.name, Text = item.id + item.name, Tag = item.id, }; foreach (var item1 in data.Where(x => x.id.Length == 5 && x.id.StartsWith(item.id))) { TreeNode tn1 = new TreeNode() { Name = item1.name, Text = item1.id + item1.name, Tag = item1.id, }; foreach (var item2 in data.Where(x => x.id.Length == 8 && x.id.StartsWith(item1.id))) { TreeNode tn2 = new TreeNode() { Name = item2.name, Text = item2.id + item2.name, Tag = item2.id, }; tn1.Nodes.Add(tn2); } tn.Nodes.Add(tn1); } treeView1.Nodes.Add(tn); } } } }
------解决方案--------------------
... string datastr = ""; foreach (DataRow dr in ds.Tables[0].Rows) { datastr += dr["GroupID"].ToString() + " " + dr["GroupName"].ToString() + "\r\n"; } //之后再套用我下面的代码 var data = datastr.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries) .Select(x => new { id = x.Split(' ')[0], name = x.Split(' ')[1] }); ...
------解决方案--------------------