نمایش ساختار درختی سلسله مراتبی در asp.net webforms ورژن ۴٫۵
string str_ConnectionStrings;
protected void Page_Load(object sender, EventArgs e)
{
str_ConnectionStrings = ConfigurationManager.ConnectionStrings[“Atlas_PublicConnectionString”].ConnectionString;
BindTreeViewControl();
}
DataSet P_Dataset(string str_SQL)
{
SqlDataAdapter adp = new SqlDataAdapter(str_SQL, str_ConnectionStrings);
DataSet obj_DataSet = new DataSet();
adp.Fill(obj_DataSet, “TableName”);
return obj_DataSet;
}
private void BindTreeViewControl()
{
try
{
DataSet obj_DataSet = P_Dataset(“Select id,name,pid from z_tbl_Tree”);
DataRow[] Rows = obj_DataSet.Tables[0].Select(“pid = 0”);
for (int i = 0; i < Rows.Length; i++)
{
TreeNode root = new TreeNode(Rows[i][“name”].ToString(), Rows[i][“id”].ToString());
root.SelectAction = TreeNodeSelectAction.Expand;
CreateNode(root, obj_DataSet.Tables[0]);
treeviwExample.Nodes.Add(root);
}
GridView1.DataSource = obj_DataSet;
GridView1.DataBind();
}
catch (Exception Ex) { throw Ex; }
}
public void CreateNode(TreeNode node, DataTable Dt)
{
DataRow[] Rows = Dt.Select(“pid =” + node.Value);
if (Rows.Length == 0) { return; }
for (int i = 0; i < Rows.Length; i++)
{
TreeNode Childnode = new TreeNode(Rows[i][“name”].ToString(), Rows[i][“id”].ToString());
Childnode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(Childnode);
CreateNode(Childnode, Dt);
}
}