一个很苯的方法实现TreeView节点的动态添加

我是个菜鸟,第一次到公司头做东西,上头叫我做一和windows资源管理器差不多的控件。
要从数据库中读取某列的值帮定上去,让后对帮定的列再帮定列对应的节点文本作为筛选条件,选出的
数据到改节点的子节点。
赶时间,将界面给用户看,没去多想。

public void createNode( )
  {
   try
   {
    string select = "select distinct City from MoiveSatr ";  //MoiveSatr表中,按city分类
    string connection =@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=123;Data Source=HUTIAN\HUTIAN;";
    string CityList  = "城市";
    DataTable tabCity = nameList.selectName(select,connection,CityList ).Tables["城市"];
                //public DataSet selectName(string strSql ,string strConn, string tabName )

    foreach(DataRow rowTest in tabCity.Rows )
    {
     TreeNode tndCity = new TreeNode( );
     tndCity.Text = rowTest["City"].ToString();
     trvStar.Nodes.Add(tndCity);

     cityName = tndCity.Text ;
     string selectName = "select name from MoiveExec where City ='";
     selectName = selectName + cityName ;
     selectName = selectName +"'";

      DataTable tabName = nameList.selectName(selectName,connection,"名字").Tables["名字"];

     foreach(DataRow rowName in tabName.Rows )
     {
      TreeNode tndName = new TreeNode( );
      tndName.Text = rowName["name"].ToString();
      tndCity.Nodes.Add(tndName);
     }
    }
   }
   catch(SqlException e)
   {
    labError.Text = e.ToString();
   }
  }

我知道上边有很多不合理,希望有高手能指点。

posted @ 2006-05-14 15:14  tianhu  阅读(689)  评论(2编辑  收藏  举报