• 动态绑定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("连接错误");
            }
          
        }
    }
  • 相关阅读:
    PostMan-NewMan运行参数
    shell脚本学习简单记录笔记
    android开发okhttp-4.9.1源码大致流程解读
    android开发获取键盘高度以及判断键盘是否显示(兼容分屏模式)
    Android开发The style on this component requires your app theme to be Theme.AppCompat (or a descendant)的解决方法
    Linux开发Ubuntu安装man手册
    Android开发源码解读四大组件源码解读简单梳理
    Android开发涉及到的AMS类和ActivityThread类源码解读
    Android开发为什么主线程可以一直运行而不会退出来
    前端CryptoJS加密、后端解密代码实现参考
  • 原文地址:https://www.cnblogs.com/wangyhua/p/4050687.html
Copyright © 2020-2023  润新知