c# treeView 动态添加子节点的问题
winform手动添加了根节点AAA,BBB,CCC。数据库里面结构如下:idChildTreeParentTree112AAA213AAA322BBB423BBB532...
winform
手动添加了根节点AAA,BBB,CCC。数据库里面结构如下:
id ChildTree ParentTree
1 12 AAA
2 13 AAA
3 22 BBB
4 23 BBB
5 32 CCC
6 33 CCC
父节点是固定的,子节点的数据是不定的,但是记录里面有父节点的Text,怎么动态的给这几个固定的父亲添加子节点啊 展开
手动添加了根节点AAA,BBB,CCC。数据库里面结构如下:
id ChildTree ParentTree
1 12 AAA
2 13 AAA
3 22 BBB
4 23 BBB
5 32 CCC
6 33 CCC
父节点是固定的,子节点的数据是不定的,但是记录里面有父节点的Text,怎么动态的给这几个固定的父亲添加子节点啊 展开
6个回答
展开全部
Load里面添加:
int i = 0;
while (i < 7)
{
switch(Convert.ToString(i))
{
case "0":
{
treeView1.Nodes.Clear();
TreeNode myTreeNode = new TreeNode("AAA");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode, "AAA", "AAA");
}
break;
case "1":
{
TreeNode myTreeNode = new TreeNode("BBB");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode, "BBB", "BBB");
}
break;
case "2":
{
TreeNode myTreeNode = new TreeNode("CCC");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode, "CCC", "CCC");
}
break;
}
i++;
后面加:
public void CreateChildTree(TreeNode ParentNode, string NodeId, string ParentNodeText)
{
OleDbDataAdapter mycommand = new OleDbDataAdapter("select * from table where ParentTree='" + NodeId + "'", conn);//conn为数据库链接
conn.Open();
DataSet ds = new DataSet();
mycommand.Fill(ds, "table");
int i;
for (i = 0; i < ds.Tables["table"].Rows.Count; i++)
{
TreeNode myTreeNode = new TreeNode();
myTreeNode.Text = ds.Tables["table"].Rows[i].ItemArray[1].ToString();
string myTreeNodeText = myTreeNode.Text;
string ParentNodeTextS = ParentNodeText;
ParentNode.Nodes.Add(myTreeNode);
}
ds.Tables["table"].Clear();
conn.Close();
}
嘎嘎,感觉好复杂,不过我就是这样做出来了。一会儿再看看高人的做法,也学习学习
int i = 0;
while (i < 7)
{
switch(Convert.ToString(i))
{
case "0":
{
treeView1.Nodes.Clear();
TreeNode myTreeNode = new TreeNode("AAA");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode, "AAA", "AAA");
}
break;
case "1":
{
TreeNode myTreeNode = new TreeNode("BBB");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode, "BBB", "BBB");
}
break;
case "2":
{
TreeNode myTreeNode = new TreeNode("CCC");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode, "CCC", "CCC");
}
break;
}
i++;
后面加:
public void CreateChildTree(TreeNode ParentNode, string NodeId, string ParentNodeText)
{
OleDbDataAdapter mycommand = new OleDbDataAdapter("select * from table where ParentTree='" + NodeId + "'", conn);//conn为数据库链接
conn.Open();
DataSet ds = new DataSet();
mycommand.Fill(ds, "table");
int i;
for (i = 0; i < ds.Tables["table"].Rows.Count; i++)
{
TreeNode myTreeNode = new TreeNode();
myTreeNode.Text = ds.Tables["table"].Rows[i].ItemArray[1].ToString();
string myTreeNodeText = myTreeNode.Text;
string ParentNodeTextS = ParentNodeText;
ParentNode.Nodes.Add(myTreeNode);
}
ds.Tables["table"].Clear();
conn.Close();
}
嘎嘎,感觉好复杂,不过我就是这样做出来了。一会儿再看看高人的做法,也学习学习
展开全部
Load里面添加:
int
i
=
0;
while
(i
<
7)
{
switch(Convert.ToString(i))
{
case
"0":
{
treeView1.Nodes.Clear();
TreeNode
myTreeNode
=
new
TreeNode("AAA");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode,
"AAA",
"AAA");
}
break;
case
"1":
{
TreeNode
myTreeNode
=
new
TreeNode("BBB");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode,
"BBB",
"BBB");
}
break;
case
"2":
{
TreeNode
myTreeNode
=
new
TreeNode("CCC");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode,
"CCC",
"CCC");
}
break;
}
i++;
后面加:
public
void
CreateChildTree(TreeNode
ParentNode,
string
NodeId,
string
ParentNodeText)
{
OleDbDataAdapter
mycommand
=
new
OleDbDataAdapter("select
*
from
table
where
ParentTree='"
+
NodeId
+
"'",
conn);//conn为数据库链接
conn.Open();
DataSet
ds
=
new
DataSet();
mycommand.Fill(ds,
"table");
int
i;
for
(i
=
0;
i
<
ds.Tables["table"].Rows.Count;
i++)
{
TreeNode
myTreeNode
=
new
TreeNode();
myTreeNode.Text
=
ds.Tables["table"].Rows[i].ItemArray[1].ToString();
string
myTreeNodeText
=
myTreeNode.Text;
string
ParentNodeTextS
=
ParentNodeText;
ParentNode.Nodes.Add(myTreeNode);
}
ds.Tables["table"].Clear();
conn.Close();
}
int
i
=
0;
while
(i
<
7)
{
switch(Convert.ToString(i))
{
case
"0":
{
treeView1.Nodes.Clear();
TreeNode
myTreeNode
=
new
TreeNode("AAA");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode,
"AAA",
"AAA");
}
break;
case
"1":
{
TreeNode
myTreeNode
=
new
TreeNode("BBB");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode,
"BBB",
"BBB");
}
break;
case
"2":
{
TreeNode
myTreeNode
=
new
TreeNode("CCC");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode,
"CCC",
"CCC");
}
break;
}
i++;
后面加:
public
void
CreateChildTree(TreeNode
ParentNode,
string
NodeId,
string
ParentNodeText)
{
OleDbDataAdapter
mycommand
=
new
OleDbDataAdapter("select
*
from
table
where
ParentTree='"
+
NodeId
+
"'",
conn);//conn为数据库链接
conn.Open();
DataSet
ds
=
new
DataSet();
mycommand.Fill(ds,
"table");
int
i;
for
(i
=
0;
i
<
ds.Tables["table"].Rows.Count;
i++)
{
TreeNode
myTreeNode
=
new
TreeNode();
myTreeNode.Text
=
ds.Tables["table"].Rows[i].ItemArray[1].ToString();
string
myTreeNodeText
=
myTreeNode.Text;
string
ParentNodeTextS
=
ParentNodeText;
ParentNode.Nodes.Add(myTreeNode);
}
ds.Tables["table"].Clear();
conn.Close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Load里面添加:
int i = 0;
while (i < 7)
{
switch(Convert.ToString(i))
{
case "0":
{
treeView1.Nodes.Clear();
TreeNode myTreeNode = new TreeNode("AAA");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode, "AAA", "AAA");
}
break;
case "1":
{
TreeNode myTreeNode = new TreeNode("BBB");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode, "BBB", "BBB");
}
break;
case "2":
{
TreeNode myTreeNode = new TreeNode("CCC");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode, "CCC", "CCC");
}
break;
}
i++;
后面加:
public void CreateChildTree(TreeNode ParentNode, string NodeId, string ParentNodeText)
{
OleDbDataAdapter mycommand = new OleDbDataAdapter("select * from table where ParentTree='" + NodeId + "'", conn);//conn为数据库链接
conn.Open();
DataSet ds = new DataSet();
mycommand.Fill(ds, "table");
int i;
for (i = 0; i < ds.Tables["table"].Rows.Count; i++)
{
TreeNode myTreeNode = new TreeNode();
myTreeNode.Text = ds.Tables["table"].Rows[i].ItemArray[1].ToString();
string myTreeNodeText = myTreeNode.Text;
string ParentNodeTextS = ParentNodeText;
ParentNode.Nodes.Add(myTreeNode);
}
ds.Tables["table"].Clear();
conn.Close();
}
int i = 0;
while (i < 7)
{
switch(Convert.ToString(i))
{
case "0":
{
treeView1.Nodes.Clear();
TreeNode myTreeNode = new TreeNode("AAA");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode, "AAA", "AAA");
}
break;
case "1":
{
TreeNode myTreeNode = new TreeNode("BBB");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode, "BBB", "BBB");
}
break;
case "2":
{
TreeNode myTreeNode = new TreeNode("CCC");
treeView1.Nodes.Add(myTreeNode);
CreateChildTree(myTreeNode, "CCC", "CCC");
}
break;
}
i++;
后面加:
public void CreateChildTree(TreeNode ParentNode, string NodeId, string ParentNodeText)
{
OleDbDataAdapter mycommand = new OleDbDataAdapter("select * from table where ParentTree='" + NodeId + "'", conn);//conn为数据库链接
conn.Open();
DataSet ds = new DataSet();
mycommand.Fill(ds, "table");
int i;
for (i = 0; i < ds.Tables["table"].Rows.Count; i++)
{
TreeNode myTreeNode = new TreeNode();
myTreeNode.Text = ds.Tables["table"].Rows[i].ItemArray[1].ToString();
string myTreeNodeText = myTreeNode.Text;
string ParentNodeTextS = ParentNodeText;
ParentNode.Nodes.Add(myTreeNode);
}
ds.Tables["table"].Clear();
conn.Close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
简单啊 !
查询出来就是了啊!
明天给你代码哈!
马上关电了!
查询出来就是了啊!
明天给你代码哈!
马上关电了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询