笔记-TreeView动态绑定数据库(无限级联)-2011.8.18

 

//实现属性的多级目录
        //
        public string rootFT_Id = "00";
        /// 
        /// 添加根节点       
        private void AddRootCompany()
        {
            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection("Data Source=localhost;uid=sa;pwd=;Database=TW_KJ"))
            {
                con.Open();
                string strSQL = "select * from TB_Personnel_Type where FT_ID ="+ rootFT_Id;
                using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con))
                {
                    adapter.Fill(ds);
                }
                TreeNode NewNode = new TreeNode();
                NewNode.Text = ds.Tables[0].Rows[0]["FT_NAME"].ToString().Trim();
                this.treeView1.Nodes.Add(NewNode);
                InitTreeCompanyChildNode(NewNode,rootFT_Id);
            }
        }         
        public DataSet getMenuByLevel(string strFT_ID)
        {
            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection("Data Source=localhost;uid=sa;pwd=;Database=TW_KJ"))
            {
                con.Open();
                string strSQL;
                if (strFT_ID == "00")
                {
                    strSQL = "select * from TB_Personnel_Type where FT_ID like'" + "0_' and FT_UP_NO = 1";
                }
                else
                {
                    strSQL = "select * from TB_Personnel_Type where FT_ID like'" + strFT_ID.Trim() + "__'";
                }
                using (SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con))
                {
                    adapter.Fill(ds);
                }
            }
            return ds;
        }

        /// 
        /// 递归获取子节点        
        private void InitTreeCompanyChildNode(TreeNode pNode, string fatherFT_ID)
        {
            DataSet ds = getMenuByLevel(fatherFT_ID);
            DataView dataView = new DataView();
            dataView = ds.Tables[0].DefaultView;
            foreach (DataRowView drv in dataView)
            {
                string newFT_ID = drv["FT_ID"].ToString();                
                string name = drv["FT_NAME"].ToString();
                TreeNode NewNode = new TreeNode();
                //将子节点添加到父节点下面
                NewNode.Text = name;
                pNode.Nodes.Add(NewNode);
                InitTreeCompanyChildNode(NewNode,newFT_ID);
            }
        }

        private void Property_Load(object sender, EventArgs e)
        {            
            AddRootCompany();           
            
        }


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值