zoukankan      html  css  js  c++  java
  • 递归绑定TreeView

      树形菜单在平常的开发中是最常见的了,自已也练习练习,先来看两个图:

                      

         效果图                         表结构

    实现代码:

       private void Form1_Load(object sender, EventArgs e)
            {
                this.BindTree(0);
            }


            private DataSet GetData(int pid)
            {
                string strCon = @"Data Source=Agony\SQLSERVER2005;Initial Catalog=sports;Integrated Security=True";
                string sql = "SELECT * FROM sport WHERE pid=" + pid;
                SqlConnection conn = new SqlConnection(strCon);
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "sport");
                return ds;
            }

            //绑定父节点
            private void BindTree(int pid)
            {
                DataSet ds = GetData(pid);
                if (ds.Tables["sport"].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables["sport"].Rows.Count; i++)
                    {
                        TreeNode node = new TreeNode();
                        node.Text = ds.Tables["sport"].Rows[i]["sname"].ToString();
                        node.Tag = ds.Tables["sport"].Rows[i]["id"].ToString();

                        this.treeView1.Nodes.Add(node);
                        BindChildNode(node);
                    }
                }
            }

            //绑定子节点
            private void BindChildNode(TreeNode node)
            {
                TreeNode tn = node;
                int id = Convert.ToInt32(tn.Tag);
                DataSet ds = GetData(id);
                for (int i = 0; i < ds.Tables["sport"].Rows.Count; i++)
                {
                    TreeNode nod = new TreeNode();
                    nod.Text = ds.Tables["sport"].Rows[i]["sname"].ToString();
                    nod.Tag = ds.Tables["sport"].Rows[i]["id"].ToString();


                    tn.Nodes.Add(nod);
                    BindChildNode(nod);
                }
            }

  • 相关阅读:
    Codeforces 900D Unusual Sequences 容斥原理
    Codeforces 900C Remove Extra One 模拟
    Codeforces 897D. Ithea Plays With Chtholly (交互)
    Codeforces 895C
    Monkey测试环境搭建
    SDK/JDK,Shell/Shell脚本,Apache/APR ,MTK
    计算机基础——Java笔记一
    用Fiddler模拟低速网络环境(弱网)
    弱网定义
    Xcode中的几个常用文件路径
  • 原文地址:https://www.cnblogs.com/puresoul/p/1776370.html
Copyright © 2011-2022 走看看