熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#動態綁定treeview控件

2022-06-13   來源: .NET編程 

  前台只需要放一個 treeview控件就行
後台

protected void Page_Load(object sender EventArgs e)
{
if (!IsPostBack)
bindTree();
}
private void bindTree()
{
DataTable dt = thisGetTreeData();
thisFillNode(dt null);
}
private void FillNode(DataTable dt TreeNode node)
{
DataView dv = new DataView(dt);
if (node == null) //根結點
{
dvRowFilter = "parentid="; //篩選到根結點的所有子節點
}
else //不是根結點
{
dvRowFilter = "parentid=" + nodeValue + ""; //篩選對應節點的所有子節點
}
foreach (DataRowView drv in dv) //遍歷填充節點的所有子節點如果傳入的節點node為葉子節點遍歷要退出不再進行遞歸
{
TreeNode no = new TreeNode(drv["menuname"]ToString() drv["menuid"]ToString());
FillNode(dt no); //填充no節點的子節點
if (node == null)
{
thisTreeViewNodesAdd(no);
}
else
{
nodeChildNodesAdd(no);
}
}
}
private DataTable GetTreeData()
{
string strcnn = ConfigurationManagerConnectionStrings["treecnn"]ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection(strcnn))
{
SqlCommand sqlcmm = sqlcnnCreateCommand();
sqlcmmCommandText = "select * from MenuTree order by parentidmenuorder";
SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
DataSet ds = new DataSet();
daFill(ds);
return dsTables[];
}
}


From:http://tw.wingwit.com/Article/program/net/201311/14260.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.