Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How can I bind a treeview to data returned by a CTE (Common Table Expression) in SQL-2005
 

How can I bind a treeview to data returned by a CTE (Common Table Expression) in SQL-2005

Just out of curiosity is it possible to bind a treeview control to data returned from a CTE?

asher syed  Thursday, November 02, 2006 10:52 AM
The windows forms treeview control does not support data binding
Ken Tucker  Thursday, November 02, 2006 6:46 PM

Ken is right, treeview does not support databinding. But you can customize/extend it. The following links will be useful

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/custcntrlsamp3.asp
http://www.codeproject.com/cs/miscctrl/dbTree.asp

Naga Satish Rupenaguntla  Thursday, November 02, 2006 6:53 PM

ken i meant to ask if a web TreeView Control in .Net 2.0 can be used to display records from a Common Table Expression in SQL-2000 hierarchically.

Following is SQL Code used to build and return the resultset

CREATE TABLE

tblDepartments

( DepartmentID INT,

DepartmentName VARCHAR(1000),

ParentDepartmentID INT

)

INSERT INTO tblDepartments VALUES(1, 'HR', NULL)

INSERT INTO tblDepartments VALUES(2, 'IT', NULL)

INSERT INTO tblDepartments VALUES(3, 'Networking', 2)

INSERT INTO tblDepartments VALUES(4, 'Development', 2)

INSERT INTO tblDepartments VALUES(5, 'ASP.NET Development', 4)

INSERT INTO tblDepartments VALUES(6, 'J2EE Development', 4)

INSERT INTO tblDepartments VALUES(7, 'C#.NET', 5)

INSERT INTO tblDepartments VALUES(8, 'VB.NET', 5)

SELECT * FROM tblDepartments

WITH Hierarchy(DepartmentID, DepartmentName, ParentDepartmentID, HLevel)

AS

(

SELECT DepartmentID, DepartmentName, ParentDepartmentID, 0 as HLevel FROM tblDepartments

UNION ALL

SELECT SubDepartment.DepartmentID

, SubDepartment.DepartmentName,

SubDepartment.ParentDepartmentID,

HLevel + 1

FROM tblDepartments SubDepartment

INNER JOIN Hierarchy ParentDepartment

ON SubDepartment.ParentDepartmentID = ParentDepartment.DepartmentID )

SELECT DepartmentID,

DepartmentName = Replicate('.', HLevel) + DepartmentName,

ParentDepartmentID,

HLevel

FROM Hierarchy

Any help on the proper usage of CTE would be great

asher syed  Friday, November 03, 2006 5:41 AM

You can use google to search for other answers

Custom Search

More Threads

• How do I get a DataGridViewTextBoxColumn to format an integer into a phone number format?
• Forcing a virtual DataGridView to refresh its Cell values
• Newbie: simple (?) data passing from one text box to another
• Master with multiple detail records displayed in form view
• DataGridViewComboColumn resets when I press the Del key!?
• field calculated in the sql query shows in dataset, but datagridview doesn't see it
• Calling a class in other forms gives Error
• Problem with DataSet Designer
• TextBox on DataGridView = text input problems in TextBox
• Deleting columns from a data bound DataGridView.