• C# 里TreeView绑定数据库实现分类


    codes=c#]
    //从数据库中读取数据
                SqlConnection con = new SqlConnection("server=127.0.0.1\\sqlexpress;uid=sa;");
                con.Open();
                con.ChangeDatabase("STggggg");
                SqlCommand cmd = new SqlCommand("select * from 产品树 where NodeType='f'", con);
                //cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter sda = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                try
                {
                    sda.Fill(ds);
                }
                catch
                {
                }
                finally
                {
                    cmd = null;
                    con.Close();
                }
                //往TreeView中添加树节点
                //添加根节点
                TreeNode tn = new TreeNode();
                tn.Text = "所有产品";
                tn.Name = "0";//Name作为ID
                tn.Tag = "0";//Tag作为RootID
                tn.ImageIndex = 0;
                tn.SelectedImageIndex = 0;
                tv.Nodes.Add(tn);//该TreeView命名为tv
                tv.SelectedNode = tv.TopNode;
                //把其他节点加上去
                if (ds != null)
                {[/codes][codes=c#]
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        tn = new TreeNode();
                        tn.Text = dr["Product"].ToString();
                        tn.Name = dr["CateID"].ToString();//Name作为CateID
                        tn.Tag = dr["RootID"].ToString();//Tag作为RootID
                        tn.ImageIndex = 1;
                        tn.SelectedImageIndex = 1;
                        //判断是否为主节点
                        if (dr["CateID"].ToString() == dr["RootID"].ToString())
                        {
                            //主节点
                            tv.SelectedNode = tv.TopNode;
                        }
                        else
                        {
                            //其他节点
                            if (tv.SelectedNode.Name != dr["ParentID"].ToString())
                            {
                                TreeNode[] tn_temp = tv.Nodes.Find(dr["ParentID"].ToString(), true); //通过ParentID查找父节点
                                if (tn_temp.Length > 0)
                                {
                                    tv.SelectedNode = tn_temp[0]; //选中查找到的节点
                                }
                            }
                        }
                        tv.SelectedNode.Nodes.Add(tn);
                    }
                    //tv.ExpandAll();//展开TreeView
                    tv.SelectedNode = tv.TopNode; //最顶端节点选中
                }
    [/codes]

  • 相关阅读:
    python定制类详解
    python格式化
    python3和2的区别
    深度优先和广度优先遍历
    python偏函数
    python匿名函数
    android 应用能够安装在什么地方
    C语言文件操作函数
    病毒木马查杀实战第026篇:“白加黑”恶意程序研究(上)
    函数指针
  • 原文地址:https://www.cnblogs.com/googlegis/p/2978981.html
Copyright © 2020-2023  润新知