CSTrüter HomeArticlesDownloadsAbout meContact me
Modify WebControl markup via WebControlAdapter 2010-03-11 21:47:12
a quick look at how to create a windows service using C# 2010-02-28 21:48:06
How to call server-side code from client-side code, using PageMethods in ASP.net 2010-02-21 12:31:27
How to pass a set of data to an xml type in SQL 2005/8 2010-02-12 19:04:23
Some funky behaviour regarding overload Resolution of dynamic/object types 2010-02-09 17:16:52
Object orientated programming within JavaScript 2010-01-28 07:25:45
How to sort data using ASP.net (C#) and SQL 2005/8 2010-01-18 15:23:14
Quick look at some of the new features added to C# 4.0 2010-01-12 21:52:13
SQL 2008 introduced a nifty feature called Table-Valued Parameters (TVP) into its codebase 2010-01-06 22:58:25
How to page data using ASP.net (C#) and SQL 2005/8 2009-10-19 15:01:45
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
2007-02-22 12:00:00
Blog about passing parameters by reference to functions using func_get_arg(s) 2008-07-27 12:38:24
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); } } }
Codebooth my semi-community site etc (work in progress)
The company I'am currently working for as software developer.
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