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


    1. //从数据库中读取数据   
    2.             SqlConnection con = new SqlConnection("server=127.0.0.1\\sqlexpress;uid=sa;");   
    3.             con.Open();   
    4.             con.ChangeDatabase("STggggg");   
    5.             SqlCommand cmd = new SqlCommand("select * from 产品树 where NodeType='f'", con);   
    6.             //cmd.CommandType = CommandType.StoredProcedure;   
    7.             SqlDataAdapter sda = new SqlDataAdapter(cmd);   
    8.             DataSet ds = new DataSet();   
    9.             try  
    10.             {   
    11.                 sda.Fill(ds);   
    12.             }   
    13.             catch  
    14.             {   
    15.             }   
    16.             finally  
    17.             {   
    18.                 cmd = null;   
    19.                 con.Close();   
    20.             }   
    21.             //往TreeView中添加树节点   
    22.             //添加根节点   
    23.             TreeNode tn = new TreeNode();   
    24.             tn.Text = "所有产品";   
    25.             tn.Name = "0";//Name作为ID   
    26.             tn.Tag = "0";//Tag作为RootID   
    27.             tn.ImageIndex = 0;   
    28.             tn.SelectedImageIndex = 0;   
    29.             tv.Nodes.Add(tn);//该TreeView命名为tv   
    30.             tv.SelectedNode = tv.TopNode;   
    31.             //把其他节点加上去   
    32.             if (ds != null)   
    33.             {  
    1.                 foreach (DataRow dr in ds.Tables[0].Rows)   
    2.                 {   
    3.                     tn = new TreeNode();   
    4.                     tn.Text = dr["Product"].ToString();   
    5.                     tn.Name = dr["CateID"].ToString();//Name作为CateID   
    6.                     tn.Tag = dr["RootID"].ToString();//Tag作为RootID   
    7.                     tn.ImageIndex = 1;   
    8.                     tn.SelectedImageIndex = 1;   
    9.                     //判断是否为主节点   
    10.                     if (dr["CateID"].ToString() == dr["RootID"].ToString())   
    11.                     {   
    12.                         //主节点   
    13.                         tv.SelectedNode = tv.TopNode;   
    14.                     }   
    15.                     else  
    16.                     {   
    17.                         //其他节点   
    18.                         if (tv.SelectedNode.Name != dr["ParentID"].ToString())   
    19.                         {   
    20.                             TreeNode[] tn_temp = tv.Nodes.Find(dr["ParentID"].ToString(), true); //通过ParentID查找父节点   
    21.                             if (tn_temp.Length > 0)   
    22.                             {   
    23.                                 tv.SelectedNode = tn_temp[0]; //选中查找到的节点   
    24.                             }   
    25.                         }   
    26.                     }   
    27.                     tv.SelectedNode.Nodes.Add(tn);   
    28.                 }   
    29.                 //tv.ExpandAll();//展开TreeView   
    30.                 tv.SelectedNode = tv.TopNode; //最顶端节点选中   
    31.             
    作者:观海看云个人开发历程知识库 - 博客园
    出处:http://www.cnblogs.com/zhangtao/
    文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    问题001:Java软件,属于系统软件还是应用软件呢?
    Apache.Tomcat 调用Servlet原理之Class类的反射机制,用orc类解释
    CharSquence 接口的作用,多态以增强String
    eclipse环境Dynamic web module version 3.1版本的进步,简化Dynamic web object 中Servlet类的配置,不用web.xml配置<Servlet>
    tomcat.apache startup.bat闪退两种解决方法
    c++谭浩强教材教学练习例题1.2 求两数之和 为什么sum=a+b;sum的值为65538
    JSON格式自动解析遇到的调用方法问题.fromJson() ..readValue()
    shell command to replace UltraEdit
    根据内容最后一位进行排序
    利用left join 筛选B表中不包含A表记录
  • 原文地址:https://www.cnblogs.com/zhangtao/p/1441983.html
Copyright © 2020-2023  润新知