IT Community - Software Programming, Web Development and Technical Support

How to populate a Winforms treeview control from dataset

This is a discussion on How to populate a Winforms treeview control from dataset within the Operating Systems forums, part of the Computer Hardware/Software and Networking category; How to populate a Winforms treeview control from dataset Hey guys, I am trying to find out how to populate ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Computer Hardware/Software and Networking > Operating Systems

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 05-22-2009, 11:20 PM
arjkhanna arjkhanna is offline
D-Web Incredible
 
Join Date: Mar 2007
Posts: 1,949
arjkhanna is on a distinguished road
Default How to populate a Winforms treeview control from dataset

How to populate a Winforms treeview control from dataset

Hey guys, I am trying to find out how to populate a winforms 2.0 tree view control from a dataset.

Since there is no DataSource property I have no clue and my search results always turn up how to do this with ASP.NET's treeview control (which does have a DataSource property).

I'll be using the dataset passed back from the RMO object MergeSubscriberMonitor.GetSessionsSummaryDataSet Method.
__________________
A.Rajesh Khanna
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 05-22-2009, 11:21 PM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: How to populate a Winforms treeview control from dataset

Hi,

The treeview does not support databinding you will have to load the treeview manually. Here is an example




String strConn = "Server = .\\SQLEXPRESS;Database = Northwind;Integrated Security = SSPI;";
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter("Select * from Products", conn);
SqlDataAdapter daCategories = new SqlDataAdapter("Select * from Categories", conn);
da.Fill(ds, "Products");
daCategories.Fill(ds, "Categories");
ds.Relations.Add("Cat_Product", ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]);
foreach(DataRow dr in ds.Tables["Categories"].Rows)
{
TreeNode tn = new TreeNode(dr["CategoryName"].ToString());
foreach (DataRow drChild in dr.GetChildRows("Cat_Product"))
{
tn.Nodes.Add(drChild["ProductName"].ToString());
}
treeView1.Nodes.Add(tn);
}
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 05-22-2009, 11:25 PM
arjkhanna arjkhanna is offline
D-Web Incredible
 
Join Date: Mar 2007
Posts: 1,949
arjkhanna is on a distinguished road
Default Re: How to populate a Winforms treeview control from dataset

Thanks for the reply and help!!

What is this line doing? Is it adding multiple tables into the dataset or multiple fields from a table?

ds.Relations.Add("Cat_Product", ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]);
__________________
A.Rajesh Khanna
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 05-22-2009, 11:26 PM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: How to populate a Winforms treeview control from dataset

Hi,

I loaded 2 tables into my dataset. That line of code tells the dataset that the 2 tables are related with the categoryid field. When I loop through the categories tables records I can get the products in that category easily.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 05-22-2009, 11:27 PM
bluesky bluesky is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 667
bluesky is on a distinguished road
Default Re: How to populate a Winforms treeview control from dataset

Hi all,

This line is adding a relationship to the the dataset between the 'CategoryID' column of the 'Categories' table and the 'CategoryID' column of the 'Products' table. This is the dataset equivalent of a relation in a database.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 05-22-2009, 11:31 PM
arjkhanna arjkhanna is offline
D-Web Incredible
 
Join Date: Mar 2007
Posts: 1,949
arjkhanna is on a distinguished road
Default Re: How to populate a Winforms treeview control from dataset

Hi All,

Thanks guys for the help! Take care!
__________________
A.Rajesh Khanna
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7  
Old 05-22-2009, 11:37 PM
nnraja nnraja is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 144
nnraja is on a distinguished road
Default Re: How to populate a Winforms treeview control from dataset

Hi,

this bit of code worked great for me; i tried to expand on it just by adding another relationship so i could have a third level in the node, like grandparent -> parent -> child, but i ended up with nodes showing in the wrong spots. if anyone could help me out, itd be much appreciated.

foreach (DataRow dr in ds.Tables["rms"].Rows)

{

TreeNode tn = new TreeNode(dr["RM"].ToString());

foreach (DataRow drChild in dr.GetChildRows("rms_dms"))

{

TreeNode tnn = new TreeNode(drChild["DM"].ToString());

foreach (DataRow drGchild in drChild.GetChildRows("dms_sns"))

{

tnn.Nodes.Add(drGchild["SN"].ToString());

}

tn.Nodes.Add(drChild["DM"].ToString());

treeView.Nodes.Add(tnn);

}

treeView.Nodes.Add(tn);

}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8  
Old 05-22-2009, 11:40 PM
nnraja nnraja is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 144
nnraja is on a distinguished road
Default Re: How to populate a Winforms treeview control from dataset

Hi,

i guess i had problems with adding the nodes to the wrong one and placement of my curly brackets



foreach (DataRow dr in ds.Tables["rms"].Rows)

{

TreeNode tn = new TreeNode(dr["RM"].ToString());

treeView.Nodes.Add(tn);

foreach (DataRow drChild in dr.GetChildRows("rms_dms"))

{

TreeNode tnn = new TreeNode(drChild["DM"].ToString());

tn.Nodes.Add(tnn);

foreach (DataRow drGchild in drChild.GetChildRows("dms_sns"))

{

TreeNode tnnn = new TreeNode(drGchild["SN"].ToString());

tnn.Nodes.Add(tnnn);

}

}

}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9  
Old 05-22-2009, 11:42 PM
bluesky bluesky is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 667
bluesky is on a distinguished road
Default Re: How to populate a Winforms treeview control from dataset

Hi all,

The treeview does not support databinding you will have to load the treeview manually. Here is an example





String strConn = "Server = .\\SQLEXPRESS;Database = Northwind;Integrated Security = SSPI;";
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter("Select * from Products", conn);
SqlDataAdapter daCategories = new SqlDataAdapter("Select * from Categories", conn);
da.Fill(ds, "Products");
daCategories.Fill(ds, "Categories");
ds.Relations.Add("Cat_Product", ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]);
foreach(DataRow dr in ds.Tables["Categories"].Rows)
{
TreeNode tn = new TreeNode(dr["CategoryName"].ToString());
foreach (DataRow drChild in dr.GetChildRows("Cat_Product"))
{
tn.Nodes.Add(drChild["ProductName"].ToString());
}
treeView1.Nodes.Add(tn);
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Googlemaps in VB.NET WinForms Application svenus VB.NET Programming 1 10-02-2009 01:13 AM
How to Populate menu using sitemap and asp .net menu control and c#: Archer C# Programming 3 04-29-2008 09:23 PM
Treeview control problem: Selected node is not retained when parent node is closed. bluesky ASP and ASP.NET Programming 1 01-23-2008 04:11 AM
Can anybody explain about the differences between dataset.clone and dataset.copy? kingmaker C# Programming 2 08-27-2007 11:22 PM
I have a TreeView control in one of my webpages and I am facing the following problem itbarota C# Programming 1 07-27-2007 04:33 AM


All times are GMT -7. The time now is 06:39 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0