TreeView动态生成多层树结构

    1、TreeView动态生成多层树结构;

    public static string ConnectionString = "Data Source=ORCL18;user =delit;password=delit;";
    public static OracleConnection con = new OracleConnection(ConnectionString);
    DataSet ds;
    DataSet ds1;
    DataTable dt = null;
    DataTable dt1 = null;
    public string stry = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            con.Open();
            this.createDataSet();
            this.createData();
            con.Close();
            //this.initTree(TreeView1.Nodes, "1");
            initTreeBegin();
            //TreeView1.CheckedNodes

        }
    }

    private DataSet createDataSet()
    {
        ds = new DataSet();
        string sqlstr = "select DEPT_ID,UPPER_ID,SORT_ORDER,DEPT_NAME,DEPT_LEVEL from sa_dept_dict order by dept_level ,SORT_ORDER";
        OracleDataAdapter dataAda = new OracleDataAdapter(sqlstr, con);
        dataAda.Fill(ds);
        return ds;
    }
    private DataSet createData()
    {
        ds1 = new DataSet();
        string sqlstr1 = "select a.*,b.user_name from sa_staff_role a left join sa_staff_info b on a.user_id=b.id   order by a.dept_id ,a.sort_order ";
        OracleDataAdapter dataA = new OracleDataAdapter(sqlstr1, con);
        dataA.Fill(ds1);
        dt1 = ds1.Tables[0];
        return ds1;
    }
    ///


    /// 取出第首节点;
    ///

    private void initTreeBegin()
    {
        dt = ds.Tables[0];
        DataRow[] drs=  dt.Select("upper_id=-1");
        TreeNode firstNode = new TreeNode();
        firstNode.Text = drs[0]["DEPT_NAME"].ToString();
        firstNode.Value = drs[0]["dept_id"].ToString();
        this.TreeView1.Nodes.Add(firstNode);
        initNodeChild(firstNode, Convert.ToInt32( drs[0]["dept_id"]));
    }
    ///
    /// 循环取出所有节点;
    ///

    ///
    ///
    private void initNodeChild(TreeNode node,int id)
    {
       
        DataRow[] drs = dt.Select(string.Format("upper_id={0}", id));
       
        for (int i = 0; i < drs.Length; i++)
        {
            TreeNode node1 = new TreeNode();          
            node1.Text = drs[i]["DEPT_NAME"].ToString();
            node1.Value = drs[i]["dept_id"].ToString();
            //stry = node1.Value;
            node.ChildNodes.Add(node1);
            initNodeChild(node1, Convert.ToInt32(drs[i]["dept_id"]));
        }
        //取出每个部门下每个人员;
        DataRow[] drs1 = dt1.Select(string.Format("dept_id={0}", id));
        for (int i = 0; i < drs1.Length; i++)
        {
            TreeNode node2 = new TreeNode();
            node2.Text = drs1[i]["user_name"].ToString();
            node2.Value = drs1[i]["user_id"].ToString();
            node.ChildNodes.Add(node2);
        }
    }

 

运行结果:

 

 

你可能感兴趣的:(oracle)