未在树视图中加载的孙子节点:Windows窗体 [英] Grandchildnodes not loading in treeview : Windows Forms
问题描述
我正在使用树视图来填充sql表中的数据。我在一个表中有父节点和子节点,在另一个表中有大节点节点。
以下是我的代码:
string groupmaster = 从aGroupMaster中选择GroupID,GroupDesc,ParentGroupID ;
string accountmaster = 从aaccountmaster中选择* 跨度>;
SqlCommand groupmastercmd = new SqlCommand(groupmaster,DBConnection.Getconnection());
SqlCommand accountmastercmd = new SqlCommand(accountmaster,DBConnection.Getconnection());
groupmastercmd.CommandType = CommandType.Text;
accountmastercmd.CommandType = CommandType.Text;
SqlDataAdapter groupmasterda = new SqlDataAdapter(groupmastercmd);
SqlDataAdapter accountmasterda = new SqlDataAdapter(accountmastercmd);
DataSet ds = new DataSet();
groupmasterda.Fill(ds, groupmasterdetails);
accountmasterda.Fill(ds, accountmasterdetails);
ds.Relations.Add( test,ds.Tables [ groupmasterdetails]。列[ GroupID],
ds.Tables [ groupmasterdetails].Columns [ ParentGroupID]);
ds.Relations.Add( test1,ds.Tables [ groupmasterdetails]。列[ GroupID],
ds.Tables [ accountmasterdetails].Columns [ GroupID]);
foreach (DataRow dr in ds.Tables [ groupmasterdetails]。行)
{
TreeNode tn = new TreeNode(dr [ GroupDesc]。ToString());
foreach (DataRow drChild in dr.GetChildRows( test))
{
tn.Nodes.Add(drChild [ GroupDesc]。ToString());
}
treeView1.Nodes [ 0 ]。Nodes.Add(tn);
}
以下链接将带您到我的树视图的示例图像。
http://imgur.com/wY8vDoZ
在这个例子中,我标记了父节点。
我有2个问题需要解决:
1.我可以得到父节点和子节点到树视图但无法获取其中的孙子节点。
2.子节点再次显示为父节点。示例以绿色框标记。
请尽快告诉我一些解决方案。
谢谢
Rahul
大家好,
搞砸了。
由以下链接提供。
http://www.c-sharpcorner.com/UploadFile/c5c6e2/populate-a-treeview-dynamically/
大家好,
搞砸了。
由以下链接提供。
http://www.c-sharpcorner.com/UploadFile/c5c6e2/populate-a-treeview-dynamically/
在我的博客中查找完整代码:
http://rahulkrishnanr.blogspot.com/2013/12/load-treeview-from-datasource-with.html
Hi,
I am using a treeview to populate data from an sql table. I have Parent and Child Nodes in one table and Grand Child Nodes in another table.
Below is my code :
string groupmaster = "select GroupID,GroupDesc,ParentGroupID from aGroupMaster";
string accountmaster = "select * from aaccountmaster";
SqlCommand groupmastercmd = new SqlCommand(groupmaster, DBConnection.Getconnection());
SqlCommand accountmastercmd = new SqlCommand(accountmaster, DBConnection.Getconnection());
groupmastercmd.CommandType = CommandType.Text;
accountmastercmd.CommandType = CommandType.Text;
SqlDataAdapter groupmasterda = new SqlDataAdapter(groupmastercmd);
SqlDataAdapter accountmasterda = new SqlDataAdapter(accountmastercmd);
DataSet ds = new DataSet();
groupmasterda.Fill(ds, "groupmasterdetails");
accountmasterda.Fill(ds, "accountmasterdetails");
ds.Relations.Add("test", ds.Tables["groupmasterdetails"].Columns["GroupID"],
ds.Tables["groupmasterdetails"].Columns["ParentGroupID"]);
ds.Relations.Add("test1", ds.Tables["groupmasterdetails"].Columns["GroupID"],
ds.Tables["accountmasterdetails"].Columns["GroupID"]);
foreach (DataRow dr in ds.Tables["groupmasterdetails"].Rows)
{
TreeNode tn = new TreeNode(dr["GroupDesc"].ToString());
foreach (DataRow drChild in dr.GetChildRows("test"))
{
tn.Nodes.Add(drChild["GroupDesc"].ToString());
}
treeView1.Nodes[0].Nodes.Add(tn);
}
The below link will take you to an example image of my treeview.
http://imgur.com/wY8vDoZ
In this example, I have marked the parent nodes.
I have 2 problems to be solved here :
1. I could get the parent nodes and child nodes to the treeview but cannot get the grandchildnodes in it.
2. The child nodes are again appearing as parent node. Example is marked in green box.
Please advise me some solutions ASAP.
Thanks
Rahul
Hi Guys,
Got it solved.
Courtesy to the link below.
http://www.c-sharpcorner.com/UploadFile/c5c6e2/populate-a-treeview-dynamically/
Hi Guys,
Got it solved.
Courtesy to the link below.
http://www.c-sharpcorner.com/UploadFile/c5c6e2/populate-a-treeview-dynamically/
Find the full code in my blog:
http://rahulkrishnanr.blogspot.com/2013/12/load-treeview-from-datasource-with.html
这篇关于未在树视图中加载的孙子节点:Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!