当前位置:编程学习 > C#/ASP.NET >>

求救高手!关于C# treeview和数据库的问题!

两周前接到一个任务,需要搞一个根据数据库动态生成一个树形视图,并且能够编辑结点,
好像要用到无限递归思想
比如:
数据表:
id     name     parentid
1      qq       0
2      ww       0
3      rr       0
4      er       1
5      kj       3


大致能生成一个树形视图出来,最好能够有源码呀!
搞了两周没搞出来,求救啊! --------------------编程问答-------------------- 你还发了两个帖子?

参考这个帖子吧,你们俩问题是类似的:
http://topic.csdn.net/u/20100505/08/afda7156-fe7e-49a1-8bd2-65a0c0a4f449.html?6124 --------------------编程问答-------------------- 奥,我用的语言是c#啊,看不懂那语言 --------------------编程问答-------------------- 这里可以直接转换成C#的,把代码拷贝过去,点击转换就好:
http://www.developerfusion.com/tools/convert/vb-to-csharp/ --------------------编程问答--------------------

public void Initial()
        {
            ArrayList lstmainkey = new ArrayList();
            ArrayList lstmaintext = new ArrayList();
            ArrayList lstchildkey = new ArrayList();
            ArrayList lstchildparent = new ArrayList();
            ArrayList lstchildtext = new ArrayList();

            lstkey.Clear();
            lstparent.Clear();
            lsttext.Clear();
            this.Nodes.Clear();

            if (this.BindingSource != null)
            {
                DataView view = this.BindingSource.List as DataView;
                if (view == null)
                {
                    return;
                }
                DataTable dt = view.ToTable();
                if (dt == null)
                {
                    return;
                }
                int nodecount = dt.Rows.Count;
                for (int i = 0; i < nodecount; i++)
                {
                    lstkey.Add(dt.Rows[i][this.KeyField]);
                    lstparent.Add(dt.Rows[i][this.ParentField]);
                    lsttext.Add(dt.Rows[i][this.TextField]);
                }
                for (int i = 0; i < nodecount; i++)
                {
                    if (lstkey[i].ToString() != lstparent[i].ToString())
                    {
                        if (lstparent[i].ToString() == string.Empty)
                        {
                            lstmainkey.Add(lstkey[i]);
                            lstmaintext.Add(lsttext[i]);
                        }
                        else
                        {
                            lstchildkey.Add(lstkey[i]);
                            lstchildparent.Add(lstparent[i]);
                            lstchildtext.Add(lsttext[i]);
                        }
                    }
                }
                int mainnodecount = lstmainkey.Count;
                TreeNode[] nodemain = new TreeNode[mainnodecount];

                for (int i = 0; i < mainnodecount; i++)
                {
                    nodemain[i] = new TreeNode();
                    nodemain[i].Text = lstmaintext[i].ToString();
                    nodemain[i].Name = lstmainkey[i].ToString();
                    this.Nodes.Add(nodemain[i]);
                }

                int childnodecount = lstchildkey.Count;
                TreeNode[] nodechild = new TreeNode[childnodecount];
                for (int i = 0; i < childnodecount; i++)
                {
                    nodechild[i] = new TreeNode();
                    nodechild[i].Text = lstchildtext[i].ToString();
                    nodechild[i].Name = lstchildkey[i].ToString();
                }

                for (int i = 0; i < childnodecount; i++)
                {
                    for (int j = 0; j < mainnodecount; j++)
                    {
                        if (lstchildparent[i].ToString() == lstmainkey[j].ToString())
                        {
                            nodemain[j].Nodes.Add(nodechild[i]);
                        }
                    }
                    for (int k = 0; k < childnodecount; k++)
                    {
                        if (lstchildparent[i].ToString() == lstchildkey[k].ToString())
                        {
                            nodechild[k].Nodes.Add(nodechild[i]);
                        }
                    }
                }
                this.ExpandAll();
            }

        }


偶写的treeview自定义控件,核心代码发给你参考参考 --------------------编程问答--------------------

public void AddTree(int ParentID, TreeNode pNode)
    {
        DataView dvTree = new DataView(ds.Tables[0]);
        //过滤ParentID,得到当前的所有子节点
        dvTree.RowFilter = "[rootid] = " + ParentID;
        foreach (DataRowView Row in dvTree)
        {
            TreeNode Node = new TreeNode();
            string k = Row["url"].ToString();
            Node.Text = Row["title"].ToString();
            if (k.Length > 2)
            {
                Node.NavigateUrl = Row["url"].ToString();
                Node.Target = "main";
            }
            else
            {
                Node.SelectAction = TreeNodeSelectAction.Expand;
            }
            Node.ImageUrl = Row["ico"].ToString();
            string zhankai=Row["zhankai"].ToString().ToLower();
            if (zhankai == "true")
            {
                Node.Expanded = true;
            }
            else
            {
                Node.Expanded = false;
            }

               
            if (pNode == null)
            {    //添加根节点
                
                this.TreeView1.Nodes.Add(Node);
               
              
                AddTree(Int32.Parse(Row["id"].ToString()), Node); //再次递归
            }
            else
            {  //添加当前节点的子节点
                //Node.Text = Row["title"].ToString();
                //Node.NavigateUrl = Row["url"].ToString();
                pNode.ChildNodes.Add(Node);
                //if (zhankai == "true")
                //{
                //    Node.Expanded = true;
                //}
                //else
                //{
                //    Node.Expanded = false;
                //}
                AddTree(Int32.Parse(Row["id"].ToString()), Node); //再次递归
            }
        }
    }
--------------------编程问答--------------------  protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack){
        string pd = Session["zhenORjia"].ToString();
        if(pd=="zhen"){
        string zhuanyeSQL = "select 专业 from tongji group by 专业";
        
        if (!IsPostBack)
        {
            for (int i = 7; i < 12; i++)
            {
                TreeNode a = new TreeNode();
                if (i < 10)
                {
                    a.Text = "0" + i + "届";
                    foreach (string shuzhi in Util.listZhuanye(zhuanyeSQL))
                    {
                        TreeNode node = new TreeNode();
                        node.Text = shuzhi;

                        a.ChildNodes.Add(node);
                        string xinshuzhi = shuzhi;
                        if (shuzhi == "计算机应用技术")
                        {
                            xinshuzhi = "计应";
                        }
                        if (shuzhi == "艺术设计")
                        {
                            xinshuzhi = "艺设";
                        }
                        if (shuzhi == "软件技术")
                        {
                            xinshuzhi = "软件";
                        }
                        if (shuzhi == "网络技术")
                        {
                            xinshuzhi = "网络";
                        }
                        string banjiSQL = "select 班级 from tongji where 班级 like '%" + xinshuzhi + "0" + i + "%' group by 班级";
                        foreach (string zishuzhi in Util.listClass(banjiSQL))
                        {
                            TreeNode zinode = new TreeNode();
                            zinode.Text = zishuzhi;
                           // Session["jie"] = "0" + i;
                            zinode.NavigateUrl = "~/select.aspx?class=" + zishuzhi + "&zhuanye=" + shuzhi;
                            node.ChildNodes.Add(zinode);
                        }
                        node.CollapseAll();
                    }
                }
                else
                {
                    a.Text = i + "届";
                    foreach (string shuzhi in Util.listZhuanye(zhuanyeSQL))
                    {
                        TreeNode node = new TreeNode();
                        node.Text = shuzhi;
                        a.ChildNodes.Add(node);
                        string xinshuzhi = shuzhi;
                        if (shuzhi == "计算机应用技术")
                        {
                            xinshuzhi = "计应";
                        }
                        if (shuzhi == "艺术设计")
                        {
                            xinshuzhi = "艺设";
                        }
                        if (shuzhi == "软件技术")
                        {
                            xinshuzhi = "软件";
                        }
                        if (shuzhi == "网络技术")
                        {
                            xinshuzhi = "网络";
                        }
                        string banjiSQL = "select 班级 from tongji where 班级 like '%" + xinshuzhi + i + "%' group by 班级";
                        foreach (string zishuzhi in Util.listClass(banjiSQL))
                        {
                            TreeNode zinode = new TreeNode();
                            zinode.Text = zishuzhi;
                            Session["jie"] =  i;
                            zinode.NavigateUrl = "~/select.aspx?class=" + zishuzhi + "&zhuanye=" + shuzhi ;
                            node.ChildNodes.Add(zinode);
                        }
                        node.CollapseAll();
                    }
                }
                TreeView1.Nodes.Add(a);
                TreeView1.CollapseAll();
            }
        }

看下吧 希望你能看懂 这是我以前弄的一个项目 主要就是在数据库中读信息 然后自动编辑节点 
--------------------编程问答-------------------- 很好! --------------------编程问答--------------------
引用 5 楼 junjie94bei 的回复:
C# code 

public void AddTree(int ParentID, TreeNode pNode)
    {
        DataView dvTree = new DataView(ds.Tables[0]);
        //过滤ParentID,得到当前的所有子节点
        dvTree.RowFilter = "[rootid] = " +……


写得很详细,可以把数据库 的数据表截图发过来看看吗,还是看不太懂啊,不方便的话我有邮箱 864398496@qq.com
最好是能把源程序发一下,谢谢啦! --------------------编程问答--------------------
引用 6 楼 a312983516 的回复:
protected void Page_Load(object sender, EventArgs e)
  {
  if(!IsPostBack){
  string pd = Session["zhenORjia"].ToString();
  if(pd=="zhen"){
  string zhuanyeSQL = "select 专业 from tongji group by ……


写得很详细,可以把数据库 的数据表截图发过来看看吗,还是看不太懂啊,不方便的话我有邮箱 864398496@qq.com
最好是能把源程序及数据库 的数据表截图发一下,谢谢啦! 
 
--------------------编程问答--------------------
引用 6 楼 a312983516 的回复:
protected void Page_Load(object sender, EventArgs e)
  {
  if(!IsPostBack){
  string pd = Session["zhenORjia"].ToString();
  if(pd=="zhen"){
  string zhuanyeSQL = "select 专业 from tongji group by ……


写得很详细,可以把数据库 的数据表截图发过来看看吗,还是看不太懂啊,不方便的话我有邮箱 864398496@qq.com
最好是能把源程序及数据库 的数据表截图发一下,谢谢啦! 
 
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,