C# 递归算法
表结构如下所示:
Num Name fatherNum BZ
01 总节点 0 ......
0101 第一个一级节点 01 ......
010101 第一个一级节点的第一个支节点 0101 ......
010102 第一个一级节点的第二个支节点 0101 ......
010103 第一个一级节点的第三个支节点 0101 ......
0102 第二个一级节点 01 ......
010201 第二个一级节点的第一个支节点 0102 ......
0103 第三个一级节点 01 ......
010301 第三个一级节点的第一个支节点 0103 ......
01030101 第三个一级节点的第一个支节点 010301 ......
使用递归方法 把数据表加载到treeview后,
怎样根据treeview上的某个节点知道它在数据库表上对应的主键值?
这是递归方法
C/S:
CodeB/S:
Num Name fatherNum BZ
01 总节点 0 ......
0101 第一个一级节点 01 ......
010101 第一个一级节点的第一个支节点 0101 ......
010102 第一个一级节点的第二个支节点 0101 ......
010103 第一个一级节点的第三个支节点 0101 ......
0102 第二个一级节点 01 ......
010201 第二个一级节点的第一个支节点 0102 ......
0103 第三个一级节点 01 ......
010301 第三个一级节点的第一个支节点 0103 ......
01030101 第三个一级节点的第一个支节点 010301 ......
使用递归方法 把数据表加载到treeview后,
怎样根据treeview上的某个节点知道它在数据库表上对应的主键值?
这是递归方法
C/S:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
1
DataSet ds = new DataSet();
2
protected void Page_Load(object sender, EventArgs e)
3
{
4
if (!IsPostBack)
5
{
6
SqlConnection conn = new SqlConnection("server=.;database=test;uid=sa;pwd=;");
7
SqlDataAdapter da = new SqlDataAdapter("select * from test", conn);
8
da.Fill(ds);
9![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
TreeNode pNode = null;
11
AddTree("0", pNode);
12
}
13![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
}
15
public void AddTree(string fatherNum, TreeNode pNode)
16
{
17
DataRow[] dr = ds.Tables[0].Select("fatherNum=" + fatherNum);
18
if (dr.Length > 0)
19
{
20
foreach (DataRow d in dr)
21
{
22
TreeNode tNode = new TreeNode();
23
tNode.Text = d["Name"].ToString();
24
if (pNode == null)
25
{
26
//添加根节点
27
TreeView1.Nodes.Add(tNode);
28
}
29
else
30
{
31
//添加当前节点的子节点
32
pNode.ChildNodes.Add(tNode);
33
tNode.Collapse();
34
}
35
AddTree(d["Num"].ToString(), tNode); //递归
36
}
37
}
38
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)