Populate a TreeView Control C#

August 27, 2009 by C#  

Populating a TreeView control in a windows application using SQL and C# is quite straightforward.

Create a self referencing table, lets call it myTable, quite orginal don't you think? ;)

CREATE TABLE [dbo].[myTable](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[title] [varchar](255) NOT NULL,
	[parentID] [int] NULL,
 CONSTRAINT [PK_myTable] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Insert some hierarchal data.

SET IDENTITY_INSERT myTable ON
GO

INSERT INTO myTable(ID, title, parentID) VALUES(1,'Microsoft', NULL)
INSERT INTO myTable(ID, title, parentID) VALUES(2,'C#', 1)
INSERT INTO myTable(ID, title, parentID) VALUES(3,'VB.net', 1)
INSERT INTO myTable(ID, title, parentID) VALUES(4,'Open Source', NULL)
INSERT INTO myTable(ID, title, parentID) VALUES(5,'Python',	4)
INSERT INTO myTable(ID, title, parentID) VALUES(6,'Ruby', 4)
INSERT INTO myTable(ID, title, parentID) VALUES(7,'PHP', 4)
INSERT INTO myTable(ID, title, parentID) VALUES(8,'Perl', 4)
INSERT INTO myTable(ID, title, parentID) VALUES(9,'Java', 4)
INSERT INTO myTable(ID, title, parentID) VALUES(10,'LinQ', 2)
INSERT INTO myTable(ID, title, parentID) VALUES(11,'5.2', 7)
INSERT INTO myTable(ID, title, parentID) VALUES(12,'4.4', 7)

GO
SET IDENTITY_INSERT myTable OFF
GO

I assume in my code example that the root parent node equals 0, hence when returning results from SQL like in the stored procedure, I use the ISNULL method to return 0 from a NULL.

You can just define a root value as well...

CREATE PROCEDURE viewMyTable
AS
BEGIN
	SELECT ID, title, ISNULL(parentID, 0) AS parentID
	FROM myTable
END

Add a same table key contraint.

ALTER TABLE [dbo].[myTable]  WITH CHECK ADD  CONSTRAINT [FK_myTable_myTable] FOREIGN KEY([parentID])
REFERENCES [dbo].[myTable] ([ID])
GO
ALTER TABLE [dbo].[myTable] CHECK CONSTRAINT [FK_myTable_myTable]


  • In your windows application, drag and drop a treeview control onto your form.
  • Populate a DataTable with your stored procedure.
Pass your TreeNodeCollection, the parentID you wish to start with and DataTable to the following method: eg. PopulateTreeView(SomeTreeView.Nodes, 0, SomeDataTable);

protected void PopulateTreeView (TreeNodeCollection parentNode, int parentID, DataTable folders)
{   
    foreach (DataRow folder in folders.Rows)
    {
        if (Convert.ToInt32(folder["parentID"]) == parentID)
        {
            String key = folder["ID"].ToString();
            String text = folder["title"].ToString();
            TreeNodeCollection newParentNode = parentNode.Add(key, text).Nodes;
            PopulateTreeView(newParentNode, Convert.ToInt32(folder["ID"]), folders);                    
        }
    }
}

If everything went according to plan, you'll end up with something like this:


Leave a Comment


Wow Fantastic ! February 9, 2013 by Dipu

Thanks dude..... U save me....

August 31, 2012 by firdaus.mansuri@gmail.com

coooooool

firdaus mansuri August 31, 2012 by Anonymous

this is very nice please keep it up and share such thing with us thanks you so much

Very Good August 31, 2012 by Welinton

Very good. Hi, I am Brazilian. I liked your example helped me a lot.

ToolTipText July 30, 2012 by Norrens

Hi, how change this code to add to all nodes ToolTipText? Regards

April 10, 2012 by koleee

Nice work!!! Thank you!

TreeView Control in C# February 22, 2012 by Pravesh Singh

This is one of the best articles so far I have read online. Just useful information. Very well presented. It helped me lot to complete my task. When I was searching an article on Treeview control over the internet, I found another nice article related to this topic which is also helped me lot. I would like share that article with you, please see at once... http://www.mindstick.com/Articles/022711d0-b15d-4dc0-b50b-e1a9b4cb6a82/?How%20to%20use%20Tree%20View%20Control%20in%20C# Thanks Everyone for your precious post.

January 27, 2012 by jeena

good one

Add Images to treenode January 20, 2012 by Anonymous

Its good, But how can i add images to childnodes

Thanks.... brother September 15, 2011 by fb/mztanvir

It helps me.... >:D<