我想显示树视图的结构 [英] I want to show structure of tree view
本文介绍了我想显示树视图的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
产品
--RawProduct
--- rawMaterial
---- rawItem
- 原材料
我想要这种树型视图
我的代码如下:
product
--RawProduct
---rawMaterial
----rawItem
--Raw Material
I want this type of tree view
my code is bellow
conn.Open();
string q = "select * from View_BOMMDP";
DataTable union = new DataTable();
OdbcDataAdapter UnionQuery = new OdbcDataAdapter("select distinct ProductName as Node ,ProductDependency as ParentNode from View_BOMMaster union select RawMaterialName as Node,ProductNameDependancy as ParentNode from View_BOMDetails", conn);
OdbcCommand cmd = new OdbcCommand(q, conn);
DataTable dt = new DataTable();
UnionQuery.Fill(union);
union.AcceptChanges();
TreeNode节点;
TreeNode node;
OdbcDataReader dr = cmd.ExecuteReader();
for (int i = 0; i < union.Rows.Count; i++)
{
if (union.Rows[i]["ParentNode"].ToString() == "")
{
node = new TreeNode(Convert.ToString(union.Rows[i]["Node"]));
string pName = Convert.ToString(union.Rows[i]["Node"]);
for (int i1 = 0; i1 < union.Rows.Count; i1++)
{
if (union.Rows[i1]["ParentNode"].ToString() ==pName)
{
node.Nodes.Add(Convert.ToString(union.Rows[i1]["Node"]));
string cNode = Convert.ToString(union.Rows[i1]["Node"]);
//for (int i11 = 0; i11 < union.Rows.Count; i11++)
//{
// if (union.Rows[i11]["ParentNode"].ToString() == cNode)
// {
// node.Nodes.Add(Convert.ToString(union.Rows[i11]["Node"]));
// TreeViewProduct.Nodes.Add(cch);
// }
//}
}
}
TreeViewProduct.Nodes.Add(node);
}
}
推荐答案
AddNodes(TreeViewProduct.Nodes, "", union);
您需要递归方法如下,未经测试:-)
you need recursive method like below, not tested :-)
private void AddNodes(TreeNodeCollection nodes, int parentNode, System.Data.DataTable dt)
{
string filterExp = string.Format("ParentNode='{0}'", parentNode);
foreach (System.Data.DataRow r in dt.Select(filterExp))
{
TreeNode item = new TreeNode()
{
Text = r[0].ToString(),
Value = r[0].ToString()
};
this.AddNodes(item.ChildNodes, int.Parse(r[1].ToString()), dt);
nodes.Add(item);
}
}
这篇关于我想显示树视图的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文