Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 populate treeview with dataset

Author  Topic 

Hommer
Aged Yak Warrior

808 Posts

Posted - 2004-08-30 : 13:01:54
Does anybody know any example code of populating windows treeview control with hierarchical dataset?

I prefer C# but vb will also do it.

I found couple samples on the web, but one uses classes, and another delegate. Both seem overkill.

My hierarchy is 3 levels deep, and the number of rows are limited. I can afford to load the treeview at the form load time.

I have been putting together following pieces, but it failed to load the tree in the relationship I want.

Thanks for any help!

private void Form1_Load(object sender, System.EventArgs e)
{
string sSQL;
SqlCommand cmd = sqlCnPubs.CreateCommand();
cmd.CommandType=CommandType.Text ;
sSQL = "Select ID, Segment from Prod_Segments";
cmd.CommandText=sSQL;
SqlDataAdapter daSeg = new SqlDataAdapter();
daSeg.SelectCommand=cmd;
DataSet dsProd=new DataSet();
daSeg.Fill (dsProd, "Segments");
sSQL = "Select ID, Catagory, SegmentID from Prod_Catagories";
cmd.CommandText=sSQL;
SqlDataAdapter daCat = new SqlDataAdapter();
daCat.SelectCommand=cmd;
DataSet dsCatagories=new DataSet();
daCat.Fill (dsProd, "Catagories");
dsProd.Relations.Add("SegtoCat", dsProd.Tables["Segments"].Columns["ID"], dsProd.Tables["Catagories"].Columns["SegmentID"]);

foreach (DataRow rowSeg in dsProd.Tables["Segments"].Rows)
{
TreeNode nodeSeg = new TreeNode();
nodeSeg.Text=rowSeg["Segment"].ToString();
nodeSeg.ImageIndex=0;
tvwProd.Nodes.Add(nodeSeg);
foreach (DataRow rowCat in rowSeg.GetChildRows("SegtoCat"))
{
TreeNode nodeCat = new TreeNode();
nodeCat.Text=rowCat["Catagory"].ToString();
tvwProd.Nodes.Add(nodeCat);
}
}

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-08-30 : 14:18:27
well you could use DataSet.GetXml() and bind that to a treeview

Go with the flow & have fun! Else fight the flow :)
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2004-08-30 : 17:23:09
Looks to me like you need to add nodeCat to nodeSeg, not to the treeview control.

-Chad

http://www.clrsoft.com

Software built for the Common Language Runtime.
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2004-08-31 : 09:25:42
Good point Chad, it did fix the problem!
I also later found out that. But my bigger challenge lies in the area of creating a recursive function.
All the samples I have rely on the underlying tables to be recursive, which is having parent and child records in the same table. We all know those kind of parent/child relationships are less common than one-to-many.
Anyway, I am working on coming up a function to replace those foreach loops. The calls to create the dataset also have rooms for improvement.
If I come up with some thing I am happy with, I will share it here.
Go to Top of Page
   

- Advertisement -