根据数据库创建TreeView的方法




数据库结构     ID int(4)  主键    ParentID int(4)     ConText nvchar(50) 均No Null
***********************************************************************
函数说明
public void AddTree(int ParentID, TreeNode pNode,TreeView treeView,DataTable dt)
其中treeView为TreeView控件,dt为数据集中的表。

调用示例:

            dt = (DataTable)td.RetiereTree();
            AddTree(0, (TreeNode)null,treeView1,dt);

Vs2005 C# WindowsXp 测试通过。
***********************************************************************

None.gif   public   void  AddTree( int  ParentID, TreeNode pNode,TreeView treeView,DataTable dt)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif           
InBlock.gif            DataView dvTree 
= new DataView(dt);
InBlock.gif            
//过滤ParentID,得到当前的所有子节点
InBlock.gif            
//dvTree.RowFilter = "[PARENTID] = " + ParentID;
InBlock.gif
            dvTree.RowFilter = "[PARENTID]=" + ParentID;
InBlock.gif
InBlock.gif
InBlock.gif            
foreach (DataRowView Row in dvTree)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif           
InBlock.gif                TreeNode Node 
= new TreeNode();
InBlock.gif                
if (pNode == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{    //添加根节点
InBlock.gif
                    Node.Text = Row["ConText"].ToString();
InBlock.gif                    treeView.Nodes.Add(Node);
InBlock.gif                    
//Node.Expanded = true;
InBlock.gif
                    AddTree(Int32.Parse(Row["ID"].ToString()), Node,treeView,dt);    //再次递归
ExpandedSubBlockEnd.gif
                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{   //̀添加当前节点的子节点
InBlock.gif
                    Node.Text = Row["ConText"].ToString();
InBlock.gif                    pNode.Nodes.Add(Node);
InBlock.gif                    
//Node.Expanded = true;
InBlock.gif
                    AddTree(Int32.Parse(Row["ID"].ToString()), Node,treeView,dt);     //再次递归
ExpandedSubBlockEnd.gif
                }

ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }
            

转载于:https://www.cnblogs.com/CShapWinForms/archive/2006/04/15/376028.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值