求大神指教~关于treeview的问题
求大神指教~关于treeview的问题有一个类似这样的数组
string[]{20120101,20120202,20130303,20130404,20140505}
怎么动态的在一个treeview中用节点安年月日分类显示;
如 2012-01 -01
-02
-03
-02 -04
-05
2013-04 -04
private void button1_Click(object sender, EventArgs e) { string[] data = new string[] { "20120101", "20120102", "20120103", "20120204", "20120205", "20130303", "20130304", "20140505" }; TreeNodeCollection nodes = treeView1.Nodes; int nodeIndex = -1; foreach (string str in data) { string nodename = str.Substring(0, 4) + "-" + str.Substring(4, 2); if (nodes.Count > 0 && nodes[nodeIndex].Text == nodename) { continue; } TreeNode node = new TreeNode(nodename); nodes.Add(node); TreeNodeCollection childNodes = node.Nodes; foreach (string child in data) { if (child.Substring(0, 6).Equals(nodename.Replace("-", ""))) { TreeNode childNode = new TreeNode(child.Substring(6,2)); childNodes.Add(childNode); } } nodeIndex++; } }在窗体上拖一个TreeView,Button,不知道是否符合你的要求
private void AddNodes(string[]Data) { TreeNodeCollection nodes = treeView.Nodes; int YeatIndex = -1, MonthIndex = -1, DayIndex = -1; foreach (string name in Data) { if (name == null) break; string NodeName = name.Substring(0,4); if (nodes.Count > 0 && nodes[YeatIndex].Text.Substring(0,4) == NodeName) { continue; } TreeNode NewNode = new TreeNode(NodeName+"年"); nodes.Add(NewNode); TreeNodeCollection ChildNodes = NewNode.Nodes; foreach (string Month in Data) { if (Month == null) break; string MonthName=Month.Substring(4,2); if (ChildNodes.Count > 0&&ChildNodes[MonthIndex].Text.Substring(0,2)==MonthName ) { continue; } if(Month.Substring(0,4)==name.Substring(0,4)) { TreeNode ChildNode=new TreeNode(MonthName+"月"); ChildNodes.Add(ChildNode); TreeNodeCollection DayNodes = ChildNode.Nodes; foreach (string Day in Data) { if (Day == null) break; string DayName = Day.Substring(6, 2); if (DayNodes.Count > 0 &&DayNodes[DayIndex].Text.Substring(0,2)==DayName ) { continue; } if (Day.Substring(0,6) == Month.Substring(0,6)) { TreeNode DayNode = new TreeNode(DayName+"日"); DayNodes.Add(DayNode); TreeNodeCollection TimeNodes = DayNode.Nodes; foreach (string Time in Data) { if (Time == null) break; string TimeName; TimeName = Time.Substring(8, 4); if (Day.Substring(0,8) == Time.Substring(0,8)) { TreeNode TimeNode = new TreeNode(TimeName); TimeNodes.Add(TimeNode); } } } DayIndex++; } } MonthIndex++; } YeatIndex++; } }