表province结构:province_id,province_name
表city结构:city_id,city_name,province_idSqlConnection conn = new SqlConnection("server=zzy;integrated security=sspi;database=test");
        SqlDataAdapter da;
        DataSet ds = new DataSet();
        string strProvinceName = "", strProvinceId = "", strCityName = "";
        private void Form1_Load(object sender, EventArgs e)
        {
            define_dataGridView();
            string _sql = "select * from province";
            da = new SqlDataAdapter(_sql, conn);
            da.Fill(ds, "province");
            for (int i = 0; i < ds.Tables["province"].Rows.Count; i++)
            {
                strProvinceName = ds.Tables["province"].Rows[i][1].ToString();
                strProvinceId = ds.Tables["province"].Rows[i][0].ToString();
                this.treeView1.Nodes.Add(new TreeNode(strProvinceName)); //read the parentroot
                TreeNode parentRoot = new TreeNode(strProvinceName);
                string _strsql = "select * from city where province_id='" + strProvinceId + "'";
                ds.Tables.Add("city");
                da = new SqlDataAdapter(_strsql, conn);
                ds.Tables["city"].Clear();
                da.Fill(ds.Tables["city"]);
                for (int j = 0; j < ds.Tables["city"].Rows.Count; j++)
                {
                    strCityName = ds.Tables["city"].Rows[j][1].ToString();
                    parentRoot.Nodes.Add(new TreeNode(strCityName)); //read the childroot
                }
            }
        } 大家帮忙看看,非常感谢!

解决方案 »

  1.   

    for (int i = 0; i < ds.Tables["province"].Rows.Count; i++)
                {
                    strProvinceName = ds.Tables["province"].Rows[i][1].ToString();
                    strProvinceId = ds.Tables["province"].Rows[i][0].ToString();
                    TreeNode parentRoot = new TreeNode(strProvinceName);
                    string _strsql = "select * from city where province_id='" + strProvinceId + "'";
                    da.SelectCommand.CommandText = _strsql;
                    da.Fill(ds.Tables["city"]);
                    for (int j = 0; j < ds.Tables["city"].Rows.Count; j++)
                    {
                        strCityName = ds.Tables["city"].Rows[j][1].ToString();
                        parentRoot.Nodes.Add(new TreeNode(strCityName)); //read the childroot
                    }
                    this.treeView1.Nodes.Add(parentRoot);
                }
      

  2.   

    另外 你这种:strCityName = ds.Tables["city"].Rows[j][1].ToString();
    最好写成 strCityName = ds.Tables["city"].Rows[j][“字段名”].ToString();比较易读
      

  3.   

    hoho~~
             
           da.Fill(ds.Tables["city"]);
    改成 
           da.Fill(ds,"city");写太快 没注意 
      

  4.   

    为什么不一次把所有的市都读出来
    然后用datatable.Select方法筛选数据??每个省底下的市都要读数据库????
      

  5.   

    this.treeView1.Nodes.Add(new TreeNode(strProvinceName)); //read the parentroot
    TreeNode parentRoot = new TreeNode(strProvinceName);
    --〉
    这得问题
    你new 了两次
      

  6.   

    我换了个写法,成功了。不过还需要改进,因为我写的这个只支持2层树结构,呵呵!
          SqlConnection conn = new SqlConnection("server=zzy;integrated security=sspi;database=test");
            SqlDataAdapter da;
            DataSet ds = new DataSet();
            private void Form1_Load(object sender, EventArgs e)
            {
                readNodes();
                this.treeView1.ExpandAll();
            }
            private void readNodes()
            {
                string _sql = "select * from province";
                da = new SqlDataAdapter(_sql, conn);
                da.Fill(ds, "province");
                DataView dvw1 = new DataView(ds.Tables["province"]);
                int i = 0;
                foreach (DataRowView myRow1 in dvw1)
                {
                    string strProvinceName = myRow1["province_name"].ToString().Trim();
                    this.treeView1.Nodes.Add(new TreeNode(strProvinceName));//read ParentNode
      //---------------------------------------------------------------------------------------------------------------------------------------//
                    da = new SqlDataAdapter("select * from city where province_id='"+myRow1["province_id"].ToString().Trim()+"'",conn);
                    da.Fill(ds,"city");
                    DataView dvw2 = new DataView(ds.Tables["city"]);
                    foreach (DataRowView myRow2 in dvw2)
                    {
                        string strCityName = myRow2["city_name"].ToString().Trim();
                        this.treeView1.Nodes[i].Nodes.Add(new TreeNode(strCityName));//read ChildNode
                    }
                    i++;
                    ds.Tables["city"].Clear();
                    this.treeView1.SelectedNode = treeView1.Nodes[0];
                }
            }
      

  7.   

    谢谢winner8080,代码已经收到,谢谢你的帮助,非常感谢!