日期:2014-05-18  浏览次数:20475 次

TreeView 建树
有一段数据如下想建一棵树
172,173,405,415
172,173,405,416
172,172,405,417
179,208,209,2010
179,208,209,2011
....

想做出这样的效果:
|-172
| |-173
| |-405
| |-415
| |-416
| |-417
|
|
|-179
| |-206
| |-207
| |-208
| |-209
| |-2011

 




------解决方案--------------------
C# code

DataTable DataC = new DataTable();
public void CreateNodesOfParent(int iParent,TreeNode pNode) 
        {
            DataView dvwData = new DataView(DataC);
            dvwData.RowFilter =  "[UpperID] = " + iParent;
            TreeView1.AutoSelect = false;
            foreach(DataRowView Row in dvwData) 
            {
                
                if(pNode == null) 
                {
                    TreeNode zNode= new TreeNode();
                    zNode.Text=Row["Name"].ToString();
                    zNode.ID =Row["ID"].ToString();
                    zNode.NavigateUrl ="CategoryList.aspx?parent_id="+Row["ID"].ToString()+
                        "&parent_name="+Row["Name"].ToString();
                    zNode.Target ="right";
                    zNode.ImageUrl="../images/folder.gif";
                    zNode.Expanded=true;
                    TreeView1.Nodes.Add (zNode);
                    CreateNodesOfParent(Int32.Parse(Row["ID"].ToString()),zNode);
                } 
                else 
                {
                    TreeNode zNode= new TreeNode();
                    zNode.Text=Row["Name"].ToString ();
                    zNode.ID =Row["ID"].ToString ();
                    zNode.NavigateUrl ="CategoryList.aspx?parent_id="+Row["ID"].ToString()+
                        "&parent_name="+Row["Name"].ToString();
                    zNode.Target ="right";
                    zNode.ImageUrl="../images/folder.gif";
                    pNode.Nodes.Add(zNode);
                    CreateNodesOfParent(Int32.Parse(Row["ID"].ToString()),zNode);    
                }
            }            
        }


CreateNodesOfParent(0,(TreeNode)null);

------解决方案--------------------
C# code

string strs;
        string[] str=new string[4];
        TreeNode tn;
        TreeNode tn1;
        TreeNode tn2;
        TreeNode tn3;
        private void Page_Load(object sender, System.EventArgs e)
        {
            if(!IsPostBack)
            {
                using(SqlConnection conn=new SqlConnection("server=.;database=master;uid=sa;pwd=sa"))
                {
                    SqlDataAdapter da=new SqlDataAdapter("select a from p order by a",conn);
                    DataSet ds=new DataSet();
                    da.Fill(ds);
                    for(int i=0;i<ds.Tables[0].Rows.Count;i++)
                    {
                        strs=ds.Tables[0].Rows[i]["a"].ToString();
                        str=strs.Split(",".ToCharArray());
                        if(ViewState["str0"]==null||ViewState["str0"].ToString()!=str[0])
                        {
                            ViewState["str0"]=str[0];
                            tn=new TreeNode();
                            tn.Text=str[0];
                            tn.Target=str[0];
                            TreeView1.Nodes.Add(tn);
                            if(ViewState["str1"]==null||ViewState["str1"].ToString()!=str[1])
                            {
                                ViewState["str1"]=str[1];
                                tn1=new TreeNode();