请问如何用C#构建一个树型啊?要那种可以任意增加和删除节点的,后台用数据库存储树型的节点信息.请各位大侠多多指示,在下多谢了先!!!

解决方案 »

  1.   

    我是用XML来保存节点信息,反过来也可以动态生成数!
      

  2.   

    用sql存储数据。
    要的话发邮件,[email protected]
      

  3.   

    哈哈,发邮件?!太过了吧
    数据库设计成字段:ID、UpperID、Name、……
    第一层根节点的UpperID为0,初始化根节点后
    根据upperid来得到每一个根节点的子节点
    递归加载子节点就ok了public string RootCaption="部门";
    public int RootKey=0;
    public string SelNodeCaption="";
    public int SelNodeID=0; private DataTable dtDep=new DataTable(); private void InitTree()
    {
    this.Nodes.Clear(); TreeNode mNode=new TreeNode();
    mNode.Text=RootCaption;
    mNode.Tag=RootKey;
    this.Nodes.Add(mNode); DBManager.Query("vDepartment",out this.dtDep);
    if(this.dtDep!=null ) FullTreeList(mNode,RootKey); mNode.Expand(); } private void FullTreeList(TreeNode ParentNode,int ParentKey)
    {
    int iDepID=0;
    int iParentID=0;
    foreach(DataRow dr in this.dtDep.Rows)
    {
    iDepID=int.Parse(dr["ID"].ToString ());
    iParentID=int.Parse(dr["UpperID"].ToString()); if(iParentID ==ParentKey)
    {
    TreeNode mNode=new TreeNode();
    mNode.Text=dr["DepartmentName"].ToString() ;
    mNode.Tag=iDepID;
    mNode.Expand();
    ParentNode.Nodes.Add(mNode);
    if(this.SelNodeID == iDepID)
    {
    mNode.Checked = true;
    this.SelectedNode=mNode;
    }
    //find his children
    FullTreeList(mNode,(int)mNode.Tag);
    } }

    }