军长 0
师长 1
排长 2
treeview 可以排序的

解决方案 »

  1.   

    public class Tree : System.Web.UI.Page
    {
    protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
    string strcn = "your Sqlconnectionstring"; private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    SqlConnection _Sqlconnection1 = new SqlConnection(strcn);
    string str1 = "select * from t_tree";
    _Sqlconnection1.Open();
    SqlCommand _Sqlcommand1 = new SqlCommand(str1,_Sqlconnection1);
    SqlDataAdapter dr=new SqlDataAdapter(_Sqlcommand1);
    DataSet da1=new DataSet();
    dr.Fill(da1);
    //ViewState  获取状态信息的字典,
    //这些信息使您可以在同一页的多个请求间保存和还原服务器控件的视图状态。
    this.ViewState["ds"]=da1; 
    _Sqlconnection1.Close();
    Gettreestruct(0,(TreeNode)null);

    }
    /// <summary>
    /// 导航树的递归算法
    /// </summary>
    /// <param name="id"></param>
    /// <param name="t1"></param>
    public void Gettreestruct(int id,TreeNode t1)
    {
    DataSet da =(DataSet)this.ViewState["ds"]; 
    DataView trr = new DataView(da.Tables[0]);
    trr.RowFilter="[parentid]="+id;
    foreach(DataRowView row in trr)
    {
      TreeNode nod=new TreeNode();
    if(t1==null)
    {
    nod.Text=row["mytext"].ToString();
    TreeView1.Nodes.Add(nod);
    //Expanded属性决定是否展开树
    nod.Expanded = true;
    Gettreestruct(Int32.Parse(row["nodeid"].ToString()),nod);
    }
    else
    {
    nod.Text =row["mytext"].ToString();
    t1.Nodes.Add(nod);
    nod.Expanded = true;
    Gettreestruct(Int32.Parse(row["nodeid"].ToString()),nod);     
    } } }
      

  2.   

    借助TreeView的parent属性用递归算法生成。