CSTrüter HomeArticlesDownloadsAbout meContact me
How to enable the filestream feature in SQL 2008 - Alternative way to store blobs(files) via SQL 2010-08-21 19:31:56
How to create a Singleton Pattern in C# 2010-08-10 22:52:52
How to prevent that threads access shared resources concurrently via Monitor. 2010-08-06 15:31:15
A quick review of the book PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide written by Larry Ullman 2010-08-04 21:48:58
How to prevent that threads access shared resources concurrently via Mutex. 2010-08-03 14:42:36
How to stop propagation of javascript events 2010-07-25 21:59:29
Post about how Pete the web developer fixed his sitemap 2010-07-17 15:12:02
How to setup an out of process session service 2010-07-08 17:51:46
How to display/add images from/to a SQL Database 2010-07-04 23:15:15
How to register a custom URL protocol handler 2010-06-28 20:34:01
Creating a WYSIWYG textbox for your website is actually quite simple. 2007-02-01 12:00:00
Move items between two listboxes in ASP.net(C#, VB.NET) and PHP 2008-06-12 17:07:43
Firefox word wrapping issues 2008-06-09 09:51:21
Populate a TreeView control in a windows application. 2009-08-27 16:01:03
2007-02-22 12:00:00
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:
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]
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
CREATE PROCEDURE viewMyTable AS BEGIN SELECT ID, title, ISNULL(parentID, 0) AS parentID FROM myTable END
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]
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); } } }
Thank you man .. I've been working on this for a while now and you just saved my time .. KISSES
thank's this is work
Glad you came right btw the code in this link is actually ASP.net based http://www.cstruter.com/blog/264
I got it to work from this link: http://www.daniweb.com/forums/thread118395.html.
I'm using asp.net. How do you implement the aspx side? Many tks again.
Have a look at this link: http://www.cstruter.com/blog/264
Compile ok with your suggestion, but I now have problem calling/referencing from aspx. Can you share your others (perhaps better) examples? Many tks again for sharing. I've been searching days for this.
Hi there, in which Namespaces is your "TreeNodeCollection" - e.g. are you using the one from the ASP.net classes or the one for windows forms? In ASP.net you'd do something like this: TreeNode newNode = new TreeNode(text, key); parentNode.Add(newNode); I've got other (perhaps better) examples of how to achieve this though
Compile error: "No overload for method 'Add' takes '2' argument for TreeNodeCollection newParentNode = parentNode.Add(key,text).Nodes; Help please!
Super
1 2 Last / 2 Pages (12 Entries)
The company I am currently working for as software developer.
Collection of C# snippets 2010-05-22 01:06:19
Collection of MS SQL snippets 2010-05-22 00:55:15
Collection of JavaScript snippets 2010-05-22 00:37:57
Collection of ASP.net snippets 2010-05-22 00:29:56
Collection of PHP snippets 2010-05-22 00:06:45
a Parallel reference of programming languages 2009-09-10 12:48:23
a tutorial explaining how to develop a simple login using PHP and MySQL 2009-09-05 18:26:47
An article looking at adding some kind of event driven model to PHP 5 2008-07-28 12:48:09
It is very simple creating your own rss reader, the following article looks at a few methods of doing this. 2008-06-23 13:18:25
A quick reference about working with dropdown boxes (select element) in javascript. 2007-02-17 16:36:41
Collection of funny programming articles 2006-10-08 14:23:43