非递归方式装载treeview,特定母表,特定代码。(第一版)

    #region 装载树形节点
    public void Form1_Load(object sender , EventArgs e)
    {
        treeView1.Nodes.Clear();
        DataSet ds = new DataSet();

        string sqlstr1 = "select id,name,code,grade  from treeview2 where grade=1";
        DataTable dt1 = GetDataTable(sqlstr1 , "biao1");
        string sqlstr2 = "select id,name,code,grade  from treeview2 where grade=2";
        DataTable dt2 = GetDataTable(sqlstr2 , "biao2");
        //MessageBox.Show(dt2.Rows.Count.ToString());
        string sqlstr3 = "select id,name,code,grade  from treeview2 where grade=3";
        DataTable dt3 = GetDataTable(sqlstr3 , "biao3");
        string sqlstr4 = "select id,name,code,grade  from treeview2 where grade=4";
        DataTable dt4 = GetDataTable(sqlstr4 , "biao4");

        TreeNode tn1 = new TreeNode();
        for (int a = 0 ; a < dt1.Rows.Count ; a++)
        {
            tn1.Text = dt1.Rows[a]["name"].ToString();
            tn1.Tag = dt1.Rows[a]["code"];
            treeView1.Nodes.Add(tn1);
            for (int i = 0 ; i < dt2.Rows.Count ; i++)
            {
                TreeNode tn2 = new TreeNode();
                tn2.Text = dt2.Rows[i]["name"].ToString();
                tn2.Tag = dt2.Rows[i]["code"];
                tn1.Nodes.Add(tn2);
                for (int j = 0 ; j < dt3.Rows.Count ; j++)
                {
                    if (dt2.Rows[i]["code"].ToString().Substring(0 , 6) == dt3.Rows[j]["code"].ToString().Substring(0 , 6))
                    {
                        TreeNode tn3 = new TreeNode();
                        tn3.Text = dt3.Rows[j]["name"].ToString();
                        tn3.Tag = dt3.Rows[j]["code"];
                        tn2.Nodes.Add(tn3);
                        for (int s = 0 ; s < dt4.Rows.Count ; s++)
                        {
                            if (dt3.Rows[j]["code"].ToString().Substring(0 , 9) == dt4.Rows[s]["code"].ToString().Substring(0 , 9))
                            {
                                TreeNode tn4 = new TreeNode();
                                tn4.Text = dt4.Rows[s]["name"].ToString();
                                tn4.Tag = dt4.Rows[s]["code"];
                                tn3.Nodes.Add(tn4);
                            }
                        }
                    }
                }
            }
        }                        
    }        
    #endregion

你可能感兴趣的:(非递归方式装载treeview,特定母表,特定代码。(第一版))