程序实现的功能是:
http://topic.csdn.net/u/20100225/10/467c31d9-8a14-47c5-828c-b31a96cd4c97.html
____________________________________________________________________________________
高手指点下满足了需求,可惜代码中"字典"这个东西从来没接触过,看的代码一头雾水!没办法只能逐行在MSDN上查找帮助(代码中的注释基本上都是MSDN上复制的),结果还是没能理解透,感觉似懂非懂!希望高手能说说如下代码的原理。
代码如下: protected void Page_Load(object sender, EventArgs e)
{
DataView dv = GetTreeViewData();//从数据库提取数据返回一个DataView类型对象(把表中的两个列全部数据都取出来了)。 Dictionary<string, string> dic = new Dictionary<string, string>();//Dictionary(TKey, TValue)泛型类,表示键和值的集合。 for (int i = 0; i < dv.Table.Rows.Count; i++)
{
//Dictionary.ContainsKey——>表示确定Dictionary(TKey, TValue)是否包含指定的键。
if (dic.ContainsKey(dv.Table.Rows[i][0].ToString()))
{
//dic[要获取或设置值的键]
//String.Format方法—>将指定的 String 中的每个格式项替换为相应对象的值的文本等效项。
dic[dv.Table.Rows[i][0].ToString()] = string.Format("{0},{1}", dic[dv.Table.Rows[i][0].ToString()], dv.Table.Rows[i][1].ToString());
}
else
{
//Dictionary.Add()—>将指定的键和值添加到字典中。
dic.Add(dv.Table.Rows[i][0].ToString(), dv.Table.Rows[i][1].ToString());
}
} TreeNode tn, tnChild;//定义两个节点对象。 //KeyValuePair—>用指定的键和值初始化 KeyValuePair<(TKey, TValue) 结构的新实例。
foreach (KeyValuePair<string, string> kvp in dic)
{
tn = new TreeNode(kvp.Key);//KeyValuePair.Key属性—>获取键/值对中的键。 string[] array = kvp.Value.Split(',');//返回的字符串数组包含此实例中的子字符串(由指定Unicode字符数组的元素分隔) //这个循环没理解透,上面的array里面都有些什么没理解。
for (int i = 0; i < array.Length; i++)
{
tnChild = new TreeNode(array[i]);
tn.ChildNodes.Add(tnChild);
}
treeView1.Nodes.Add(tn);
}
}希望高手能说说这段代码的原理。
谢谢!!
http://topic.csdn.net/u/20100225/10/467c31d9-8a14-47c5-828c-b31a96cd4c97.html
____________________________________________________________________________________
高手指点下满足了需求,可惜代码中"字典"这个东西从来没接触过,看的代码一头雾水!没办法只能逐行在MSDN上查找帮助(代码中的注释基本上都是MSDN上复制的),结果还是没能理解透,感觉似懂非懂!希望高手能说说如下代码的原理。
代码如下: protected void Page_Load(object sender, EventArgs e)
{
DataView dv = GetTreeViewData();//从数据库提取数据返回一个DataView类型对象(把表中的两个列全部数据都取出来了)。 Dictionary<string, string> dic = new Dictionary<string, string>();//Dictionary(TKey, TValue)泛型类,表示键和值的集合。 for (int i = 0; i < dv.Table.Rows.Count; i++)
{
//Dictionary.ContainsKey——>表示确定Dictionary(TKey, TValue)是否包含指定的键。
if (dic.ContainsKey(dv.Table.Rows[i][0].ToString()))
{
//dic[要获取或设置值的键]
//String.Format方法—>将指定的 String 中的每个格式项替换为相应对象的值的文本等效项。
dic[dv.Table.Rows[i][0].ToString()] = string.Format("{0},{1}", dic[dv.Table.Rows[i][0].ToString()], dv.Table.Rows[i][1].ToString());
}
else
{
//Dictionary.Add()—>将指定的键和值添加到字典中。
dic.Add(dv.Table.Rows[i][0].ToString(), dv.Table.Rows[i][1].ToString());
}
} TreeNode tn, tnChild;//定义两个节点对象。 //KeyValuePair—>用指定的键和值初始化 KeyValuePair<(TKey, TValue) 结构的新实例。
foreach (KeyValuePair<string, string> kvp in dic)
{
tn = new TreeNode(kvp.Key);//KeyValuePair.Key属性—>获取键/值对中的键。 string[] array = kvp.Value.Split(',');//返回的字符串数组包含此实例中的子字符串(由指定Unicode字符数组的元素分隔) //这个循环没理解透,上面的array里面都有些什么没理解。
for (int i = 0; i < array.Length; i++)
{
tnChild = new TreeNode(array[i]);
tn.ChildNodes.Add(tnChild);
}
treeView1.Nodes.Add(tn);
}
}希望高手能说说这段代码的原理。
谢谢!!
大致猜想吧,是这样一个意思:有这么一个表:
列1(主键) 列2
1 a,b,c,d
2 e,f,g,h
3 i,j,k,l
然后利用字典读出内容,长成一颗树
1
-a
-b
-c
-d
2
-e
-f
-g
-h
3
-i
-j
-k
-l
key value
"1" "a"
"2" "b"
"3" "c"
"4" "d"
http://topic.csdn.net/u/20100225/15/13e1ea0b-3f90-4f87-b31d-cf8efb8aba1b.html