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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| 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... |
|
#3
| |||
| |||
| 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 |
|
#4
| |||
| |||
| 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... |
|
#5
| |||
| |||
| 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. |
|
#6
| |||
| |||
| Hi All, Thanks guys for the help! Take care!
__________________ A.Rajesh Khanna |
|
#7
| |||
| |||
| 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); } |
|
#8
| |||
| |||
| 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); } } } |
|
#9
| |||
| |||
| 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); } |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |
Our Partners |