Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 14th, 2004, 11:33 PM
Registered User
 
Join Date: Sep 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Urgent!! Populating Treeview server control

Hi xperts,
I am trying to populate a treeview server control from a single category table. My table structure is:
CatId -> pk, identity
CatName -> Name of category
ParentCatId -> Name of parent category id, 0 for root level categories, as set to default

As u can see I need to support n-level category/ subcategory.
I need to populate a tree view control to display the category tree from this table. Any suggestions/recommendations/code-example will be a great help for me. I have done a relationship type example but this does not work in this case.
Thx in advance

 
Old November 15th, 2004, 05:23 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Fortunately, .NET has some great functionality in the DataTable that makes this pretty easy. Here's how I do it.

First, select your category in this fashion:
    SELECT CatId, CatName, ParentCatId FROM category ORDER BY CatName

- Get the result into a single DataTable object.
- Use "DataTable.DefaultView.RowFilter" to set the filter that defines the root (or parent-less) nodes of your category tree. Usually this value will be null or some ID that you have defined as meaning "no parent" (perhaps 0 or -1).
    "ParentCatId=0"
- Create new TreeNode (to serve as the root node)
- Add new "RootNode" node to the tree control
- Call RecurseTree(DataTable.DefaultView, RootNode.Nodes)

Method RecurseTree(DataView, TreeNodeCollection)
For each row in the dataview
    Create new child node
    childNode name = row(CatName)
    TreeNodeCollection.Add(childNode)
    DataView.Table.DefaultView.RowFilter = "ParentCatId=" & row(CatID)
    RecurseTree(DataView.Table.DefaultView, childNode.Nodes)
Next row

With some tweaking this should work for you.
 
Old November 17th, 2004, 08:41 AM
Registered User
 
Join Date: Sep 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes planoie
Fortunately this has worked with some tweeking.
Thx for the quick help.

 
Old September 24th, 2007, 09:52 AM
Registered User
 
Join Date: Sep 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, Could you guys please show the example. I tried as it mentioned but didn't work for me.

Thanks in advance






Similar Threads
Thread Thread Starter Forum Replies Last Post
Populating TreeView Control using Access Database dotnetDeveloper ADO.NET 0 November 18th, 2008 12:58 PM
TreeView Control ugarcia BOOK: Expert One-on-One Access Application Development 0 April 20th, 2007 01:57 PM
Treeview Control Casanova1 VB.NET 2002/2003 Basics 0 January 8th, 2004 04:27 PM
urgent help for Treeview melvik C# 2 December 25th, 2003 10:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.