none
请高手帮我看看:TreeView绑定出现了什么状况? RRS feed

  • 问题

  • 本人正在学习C#,遇到了TreeView这个难点,请大神帮忙!

    数据库字段如下:

    代码:

     private void bindRoot()
            {
                TreeNode tn = new TreeNode();
                tn.Text = "所有产品";
                tn.Tag = "0";
                tn.Name = "0";
                MenuTView.Nodes.Add(tn);
                MenuTView.SelectedNode = this.MenuTView.TopNode;

                DataRow[] row = ds.Tables[0].Select("AutoCode like '0*' and ParentsID=1");
                foreach (DataRow dr in row)
                {
                    TreeNode child = new TreeNode();
                    child.Text = dr["TextName"].ToString();
                    child.Tag = dr["ParentsID"].ToString();
                    tn.Nodes.Add(child);
                    bindChild(child, dr["AutoCode"].ToString());
                }
                MenuTView.SelectedNode.ExpandAll();
            }

            private void bindChild(TreeNode pNode,string strcode)
            {
                DataRow[] drow = ds.Tables[0].Select("AutoCode like '" + strcode + "*'");
                foreach (DataRow drw in drow)
                {
                    TreeNode node = new TreeNode();
                    node.Text = drw["TextName"].ToString();
                    node.Tag = drw["ParentsID"].ToString();
                    if (MenuTView.SelectedNode != pNode.Tag)
                    {
                        TreeNode[] tn_temp = MenuTView.Nodes.Find(drw["AutoCode"].ToString(), true);
                        if (tn_temp.Length >0)
                        {
                            MenuTView.SelectedNode = tn_temp[0];
                        }
                    }
                    pNode.Nodes.Add(node);
                }
                MenuTView.SelectedNode = MenuTView.TopNode;
            }

    运行后的效果如下:

    1.以上的效果发现只绑定了两级,无法实现无限级的效果,请大神帮我看看代码哪里出了问题?

    2.我只是在学习,没有做项目,请不要让我上传什么项目资料,我可真的拿不出东西。

    3.我这里没有用递归,因为我觉得递归的效率不够快。

    4.万分感谢各位大神,祝工作顺利,生意兴隆,身体健康!
    • 已编辑 蒙田 2013年12月21日 5:49
    2013年12月21日 5:46

答案

  • 你好:

    如果想无限级的帮顶当然要使用递归的方法,我简单做了一个测试,下面的代码可以满足你的要求,你可以在这个基础上修改一下:

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public DataSet ds = new DataSet();
    
            private void Form1_Load(object sender, EventArgs e)
            {
                using (SqlConnection conn = new SqlConnection("server=localhost;uid=sa;password=password;database=db_test;"))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand("select * from t_test", conn))
                    {
                        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                        adapter.Fill(ds);
                    }
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                bindRoot();
            }
    
            private void bindRoot()
            {
                TreeNode tn = new TreeNode();
                tn.Text = "所有产品";
                tn.Tag = "0";
                tn.Name = "0";
                MenuTView.Nodes.Add(tn);
                MenuTView.SelectedNode = this.MenuTView.TopNode;
    
                DataRow[] rows = ds.Tables[0].Select("AutoCode like '0*' and ParentsID=1");
                Bind(tn, rows,1);
    
                MenuTView.SelectedNode.ExpandAll();
            }
    
            /// <summary>
            /// 绑定菜单
            /// </summary>
            /// <param name="parent">父菜单</param>
            /// <param name="rows">所有待绑定子菜单</param>
            /// <param name="parentsID">待绑定的层级</param>
            private void Bind(TreeNode parent, DataRow[] rows,int parentsID)
            {
                foreach (DataRow row in rows)
                {
                    TreeNode child = new TreeNode();
                    child.Text = row["TextName"].ToString();
                    child.Tag = row["ParentsID"].ToString();
                    parent.Nodes.Add(child);
    
                    DataRow[] drs = ds.Tables[0].Select("AutoCode like '" + row["AutoCode"].ToString() + "*' and ParentsID=" + (parentsID + 1).ToString());
                    Bind(child, drs, parentsID + 1);
                }
            }
        }

    效果如下:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2013年12月24日 2:21
    版主

全部回复

  • 你好:

    如果想无限级的帮顶当然要使用递归的方法,我简单做了一个测试,下面的代码可以满足你的要求,你可以在这个基础上修改一下:

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public DataSet ds = new DataSet();
    
            private void Form1_Load(object sender, EventArgs e)
            {
                using (SqlConnection conn = new SqlConnection("server=localhost;uid=sa;password=password;database=db_test;"))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand("select * from t_test", conn))
                    {
                        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                        adapter.Fill(ds);
                    }
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                bindRoot();
            }
    
            private void bindRoot()
            {
                TreeNode tn = new TreeNode();
                tn.Text = "所有产品";
                tn.Tag = "0";
                tn.Name = "0";
                MenuTView.Nodes.Add(tn);
                MenuTView.SelectedNode = this.MenuTView.TopNode;
    
                DataRow[] rows = ds.Tables[0].Select("AutoCode like '0*' and ParentsID=1");
                Bind(tn, rows,1);
    
                MenuTView.SelectedNode.ExpandAll();
            }
    
            /// <summary>
            /// 绑定菜单
            /// </summary>
            /// <param name="parent">父菜单</param>
            /// <param name="rows">所有待绑定子菜单</param>
            /// <param name="parentsID">待绑定的层级</param>
            private void Bind(TreeNode parent, DataRow[] rows,int parentsID)
            {
                foreach (DataRow row in rows)
                {
                    TreeNode child = new TreeNode();
                    child.Text = row["TextName"].ToString();
                    child.Tag = row["ParentsID"].ToString();
                    parent.Nodes.Add(child);
    
                    DataRow[] drs = ds.Tables[0].Select("AutoCode like '" + row["AutoCode"].ToString() + "*' and ParentsID=" + (parentsID + 1).ToString());
                    Bind(child, drs, parentsID + 1);
                }
            }
        }

    效果如下:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2013年12月24日 2:21
    版主
  • 实在是感谢了,您太历害了,祝好人一生平安!

    2013年12月24日 3:15