在c#中递归一个表到树视图 [英] recursive one table to treeview in c#
本文介绍了在c#中递归一个表到树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示可能是递归表,名称是位置,并在树状视图中有4个列ID,标题,LatinTitle,ParentID。
网上的
和我拖放一个treeview1我该怎么办?请帮助我,我是学习者
i wanna show may recursive table thats name is Location and has 4 colum ns"ID,Title,LatinTitle,ParentID" in tree view.
in web and I drag and drop a treeview1 what shall i do? please help me i'm learner
推荐答案
这是我用来使用单个递归函数为多级层次结构制作树视图模型的代码。
可以根据需要轻松修改它。
Here is the code I used to make a treeview model for upto multilevel hierarchy using a single recursive function.
It can be easily modified to use as required.
private List<TreeViewItemModel> MakeTree(IEnumerable<MyObject> list, TreeViewItemModel parentNode)
{
List<TreeViewItemModel> treeViewList = new List<TreeViewItemModel>();
var nodes = list.Where(x => parentNode == null ? x.ParentID == 0 : x.ParentID == int.Parse(parentNode.Id));
foreach (var node in nodes)
{
TreeViewItemModel newNode = new TreeViewItemModel();
newNode.Id = node.ID.ToString();
newNode.Text = node.Text;
if (parentNode == null)
{
treeViewList.Add(newNode);
}
else
{
parentNode.Items.Add(newNode);
}
BindTree(list, newNode);
}
return treeViewList;
}
从DataBase填充TreeView [ ^ ] Moinul Islam [ ^ ]工作正常,我刚检查过..这是根据我使用的本地表修改的代码..
The Example at Populate TreeView From DataBase[^] by Moinul Islam[^] is working just fine,I just checked it.. This is the modified code according to my local table that i used..
public SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
Load_tree();
}
protected DataSet PDataset(string select_statement)
{
Connect();
con.Open();
SqlDataAdapter ad = new SqlDataAdapter(select_statement, con);
DataSet ds = new DataSet();
ad.Fill(ds);
con.Close();
return ds;
}
public void Connect()
{
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["cnstring"].ConnectionString);
}
public void Load_tree()
{
DataSet PrSet = PDataset("SELECT * FROM tbl_WebMenu");
TreeView1.Nodes.Clear();
foreach (DataRow dr in PrSet.Tables[0].Rows)
{
if ((int)dr["ParentID"] == 0)
{
TreeNode tnParent = new TreeNode();
tnParent.Text = dr["MenuName"].ToString();
string value = dr["MenuID"].ToString();
tnParent.Expand();
TreeView1.Nodes.Add(tnParent);
FillChild(tnParent, value);
}
}
}
public int FillChild(TreeNode parent, string IID)
{
DataSet ds = PDataset("SELECT * FROM tbl_WebMenu WHERE ParentID =" + IID);
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode child = new TreeNode();
child.Text = dr["MenuName"].ToString().Trim();
string temp = dr["MenuID"].ToString();
child.Collapse();
parent.ChildNodes.Add(child);
FillChild(child, temp);
}
return 0;
}
else
{
return 0;
}
}
如果你想要别的东西清楚地表明它......那么递归表?! ..如果你的意思是以下结构
If you want something else state it clearly..e.g "Recursive table"?!.. if you mean following structure
Parent 1
-- Sub-Parent
--Child
Parent 2
-- Child
Parent 3
Parent 4
-- Child 1
-- Child 2
Moinul Islam的上述代码适合您。如果没有,请解释递归表
the above code by Moinul Islam is for you.. If not then explain "Recursive table"
这篇关于在c#中递归一个表到树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文