本文介绍了从SQL Server 2005将数据加载到动态树视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经深陷其中.
我有两个桌子
Hi,
I am in a deep fix.
I have two table
ParentTable
Data
ParentID ParentName
1 John
2 Chris
另一张桌子
Another Table
ChildTable
Data
ChildID ParentID ChildName
1 1 aaa
2 1 bbb
3 2 ccc
4 2 ddd
现在,我想在动态树视图中显示它
这是我的动态树.
Now I want to show it in dynamic tree view
this is my dynamic tree.
TreeView myTreeView = new TreeView();
myTreeView.Location = new Point(100, 100);
myTreeView.Size = new Size(200, 200);
this.Controls.Add(myTreeView);
void fill_Tree2()
{
DataSet PrSet = "Select * from ParentTable";
myTreeView.Nodes.Clear();
foreach (DataRow dr in PrSet.Tables[0].Rows)
{
TreeNode tnParent = new TreeNode();
tnParent.Text = dr["ParentName"].ToString();
tnParent.Value = dr["ParentID"].ToString();
// tnParent.PopulateOnDemand = true;
// tnParent.ToolTip = "Click to get Child";
// tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;
tnParent.Expand();
// tnParent.Selected = true;
myTreeView.Nodes.Add(tnParent);
FillChild(tnParent, tnParent.Value);
}
}
public void FillChild(TreeNode parent, string ParentId)
{
DataSet ds = PDataset("Select * from ChildTable where ParentId =" + ParentId);
parent.ChildNodes.Clear();
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode child = new TreeNode();
child.Text = dr["ChildName"].ToString().Trim();
child.Value = dr["ChildID"].ToString().Trim();
if (child.ChildNodes.Count == 0)
{
child.PopulateOnDemand = true;
}
// child.ToolTip = "Click to get Child";
// child.SelectAction =TreeNodeSelectAction.SelectExpand;
child.CollapseAll();
parent.ChildNodes.Add(child);
}
}
这是基于桌面的应用程序.我需要加载父级和子级ID,但不支持 node.value
我是新手开发人员,
请帮帮我.
Masud
It is desktop based application.I need parent and child id load but it is not support node.value
I am a novice developer,
Please help me.
Masud
推荐答案
void fill_Tree2()
{
DataSet PrSet = "Select * from ParentTable";
myTreeView.Nodes.Clear();
foreach (DataRow dr in PrSet.Tables[0].Rows)
{
TreeNode tnParent = new TreeNode();
tnParent.Text = dr["ParentName"].ToString();
tnParent.Tag = dr["ParentID"].ToString(); // <====== change here ============
// tnParent.PopulateOnDemand = true;
// tnParent.ToolTip = "Click to get Child";
// tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;
tnParent.Expand();
// tnParent.Selected = true;
myTreeView.Nodes.Add(tnParent);
FillChild(tnParent, (string)tnParent.Tag); // <=== change here =====
}
并在您的FillChild
方法中进行相同的更改.
and make the same change in your FillChild
method.
public void FillChild(TreeNode parent, string ParentId)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = Global.constr;
con.Open();
SqlCommand cmd2 = new SqlCommand("Select * from Dealers where Station =''" + ParentId+"''", con);
SqlDataAdapter da = new SqlDataAdapter(cmd2);
DataSet PDataset = new DataSet();
da.Fill(PDataset, "Dealers");
DataSet ds = PDataset;
parent.Nodes.Clear();
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode child = new TreeNode();
child.Text = dr["Code"].ToString().Trim();
child.Tag = dr["Code"].ToString().Trim();
if (child.Nodes.Count == 0)
{
//child.PopulateOnDemand = true;
}
// child.ToolTip = "Click to get Child";
// child.SelectAction =TreeNodeSelectAction.SelectExpand;
// child.CollapseAll();
parent.Nodes.Add(child);
}
这篇关于从SQL Server 2005将数据加载到动态树视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!