zoukankan      html  css  js  c++  java
  • 动态绑定TreeVew

    动态绑定TreeVew

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class _Default : System.Web.UI.Page
    {
        private DataView dv;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetData();
                BuilderTree(-1, (TreeNode)null);
            }     
        }

        private void GetData()
        {
            string sql = "select * from inventoryclass";
            DataSet ds = DBHelper.GetData(sql);
             dv = new DataView(ds.Tables[0]);
        }

        //树的绑定
        private void BuilderTree(int ParentID, TreeNode pNode)
        {
            TreeView1.ShowCheckBoxes = TreeNodeTypes.All;
            dv.RowFilter = "ParentID =" + ParentID;
            foreach (DataRowView row in dv)
            {
                TreeNode node = new TreeNode();
                if (pNode == null)
                {
                    //添加根节点 
                    node.Text = row["name"].ToString();
                    TreeView1.Nodes.Add(node);
                    node.Expanded = true;
                    BuilderTree(int.Parse(row["id"].ToString()), node); //递归 
                }
                else
                {
                    //添加当前节点的子节点 
                    node.Text = row["name"].ToString();
                    pNode.ChildNodes.Add(node);
                    node.Expanded = true;
                    BuilderTree(int.Parse(row["id"].ToString()), node); //递归 
                }
            }
        }
    }

     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.Data.SqlClient;
    /// <summary>
    ///DBHelper 的摘要说明
    /// </summary>
    public class DBHelper
    {
        public DBHelper()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        public static DataSet GetData(string sql)
        {
            try
            {
                SqlConnection conn = new SqlConnection(@"server=(local);database=db_13;uid=sa;pwd=13875712605;");
               
                //SqlCommand cmd = new SqlCommand(sql,conn);
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(sql,conn);
             
                DataSet ds = new DataSet();
                da.Fill(ds);
                conn.Close();
                return ds;
            }
           catch(Exception e)
            {
                throw new Exception("连接错误");
            }
          
        }
    }
  • 相关阅读:
    Introduces the basic structure of Android ActivityManagerService
    创业的本质是资源整合
    android系统的经典文章
    GUI软件框架--窗口服务器
    学习法则:只接收能够体系化的知识
    编程思想:以什么样的方式来建模、分析、思考、解决问题
    怎么从本质上理解面向对象的编程思想?
    视图的对象化描述
    DOM= Document Object Model,文档对象模型---以对象管理组织(OMG)的规约为基础的
    GUI(UI编程)语言与面向对象、dsl
  • 原文地址:https://www.cnblogs.com/wangyhua/p/4050687.html
Copyright © 2011-2022 走看看