TreeView绑定数据库

数据库:

公司:id(int)     company(varchar(50)

部门:id(int)     deid(int)     department(varchar(50))

班组:id(int)     coid(int)     class(varchar(50))

前台:

< asp:TreeView ID = " TreeView1 "  runat = " server "  ShowLines = " true "  ExpandDepth = " 0 "   >

ShowLines="true"..........................显示连接树的节点

ExpandDepth=“0”...........................展开树的深度

后台:

方法一:

         string  strConn  =  System.Configuration.ConfigurationManager.ConnectionStrings[ " rizhi " ].ConnectionString;
        
protected   void  Page_Load( object  sender, EventArgs e)
        
{
            
if (!IsPostBack)
            
{
                
using (SqlConnection conn = new SqlConnection(strConn))
                
{
                    DataSet ds_company 
= new DataSet();//实例化数据集(公司)
                    SqlDataAdapter sda_company = new SqlDataAdapter("select * from [company]", conn);//实例化数据适配器(公司)
                    sda_company.Fill(ds_company, "company");//虚拟表填充到内存
                    
for (int i = 0; i < ds_company.Tables["company"].Rows.Count; i++)//循环所有"公司"
                    {
                        TreeNode td_company 
= new TreeNode();//实例化公司节点
                        td_company.Text = ds_company.Tables["company"].Rows[i]["company"].ToString();//公司节点名称
                        TreeView1.Nodes.Add(td_company);//将公司节点添加至"树"
                        DataSet ds_department = new DataSet();//实例化数据集(部门)
                        SqlDataAdapter sda_department = new SqlDataAdapter("select * from [department] where [coid]=" + ds_company.Tables["company"].Rows[i]["id"], conn);//实例化数据库适配器(部门)
                        sda_department.Fill(ds_department, "department");//虚拟表填充到内存
                        for (int j = 0; j < ds_department.Tables["department"].Rows.Count; j++)//循环 ds.Tables["company"].Rows[i]["id"] 对应的所有部门
                        {
                            TreeNode td_department 
= new TreeNode();//实例化部门节点
                            td_department.Text = ds_department.Tables["department"].Rows[j]["department"].ToString();//部门节点名称
                            td_company.ChildNodes.Add(td_department);//填充部门节点至"树"

                            DataSet ds_class 
= new DataSet();//实例化数据集(班组)
                            SqlDataAdapter sda_class = new SqlDataAdapter("select * from [class] where [deid]=" + ds_department.Tables["department"].Rows[j]["id"], conn);//实例化数据库适配器(班组)
                            sda_class.Fill(ds_class, "class");//虚拟表填充到内存
                            for (int z = 0; z < ds_class.Tables["class"].Rows.Count; z++)//循环 ds_department.Tables["department"].Rows[j]["id"] 对应的所有部门
                            {
                                TreeNode td_class 
= new TreeNode();//实例化班组节点
                                td_class.Text = ds_class.Tables["class"].Rows[z]["class"].ToString();//班组节点名称
                                td_department.ChildNodes.Add(td_class);//填充班组节点至"树"
                            }

                        }

                    }

                }

            }

        }

    }

 方法二:

         string  strConn  =  System.Configuration.ConfigurationManager.ConnectionStrings[ " rizhi " ].ConnectionString;
        
protected   void  Page_Load( object  sender, EventArgs e)
        
{
            
if (!IsPostBack)
            
{
                DataSet ds 
= new DataSet();
                SqlConnection conn 
= new SqlConnection(strConn);
                
using (conn)
                
{
                    SqlDataAdapter sda_Company 
= new SqlDataAdapter("select [id],[company] from [company]", conn);
                    sda_Company.Fill(ds, 
"company");
                    TreeNode tn_Company;
                    TreeNode tn_Department;
                    TreeNode tn_Class;
                    
foreach (DataRow company_row in ds.Tables["company"].Rows)
                    
{
                        tn_Company 
= new TreeNode();
                        tn_Company.Text 
= company_row["company"].ToString();
                        tn_Company.Value 
= company_row["id"].ToString();
                        TreeView1.Nodes.Add(tn_Company);

                        SqlDataAdapter sda_Department 
= new SqlDataAdapter("select [id],[department] from [department] where coid=" + tn_Company.Value, conn);
                        sda_Department.Fill(ds, 
"department");
                        
if (ds.Tables["department"].Rows.Count > 0)
                        
{
                            
foreach (DataRow department_row in ds.Tables["department"].Rows)
                            
{
                                tn_Department 
= new TreeNode();
                                tn_Department.Text 
= department_row["department"].ToString();
                                tn_Department.Value 
= department_row["id"].ToString();
                                tn_Company.ChildNodes.Add(tn_Department);

                                SqlDataAdapter sda_Class 
= new SqlDataAdapter("select [id],[class] from [class] where deid=" + tn_Department.Value, conn);
                                sda_Class.Fill(ds, 
"class");
                                
if (ds.Tables["class"].Rows.Count > 0)
                                
{
                                    
foreach (DataRow class_row in ds.Tables["class"].Rows)
                                    
{
                                        tn_Class 
= new TreeNode();
                                        tn_Class.Text 
= class_row["class"].ToString();
                                        tn_Class.Value 
= class_row["id"].ToString();
                                        tn_Department.ChildNodes.Add(tn_Class);
                                    }

                                    ds.Tables[
"class"].Clear();
                                }

                                sda_Class.Dispose();
                            }

                            ds.Tables[
"department"].Clear();
                        }

                        sda_Department.Dispose();
                    }

                    sda_Company.Dispose();
                }

            }

        }

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值