Advertisement

02.11.2008 at 07:50AM PST, ID: 23153271
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Databinding on the treeview control
Tags: VB.Net, Framework 2.0, Data Binding, Treeview Control
Hi Experts,

I'm currently writing a VB.Net 2005 application on the Dot Net Framework 2.0.  I have a Dataview with about 800 records that I use to poulate a treeview with.  Each record in the dataview represents a node in the treeview.  A record can either be a parent node or a child node of another node.

Currently I populate the treeview by adding the nodes one at a time.  After adding a node, I filter the dataview to get all the child nodes of the node just added.  I then add them one by one and for each of them filter the dataview again to get the next set of child nodes.

Someone told me that it is possilble in .Net 2 to bind such a dataview to a treeview control if you have all fields that indicate which records are children of which records...  Is it possible?  How could I do this?

If required, I could post the structure of the table....

Thanks
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: PantoffelSlippers
Solution Provided By: TheLearnedOne
Participating Experts: 1
Solution Grade: B
Views: 432
Translate:
Loading Advertisement...
02.11.2008 at 08:11AM PST, ID: 20867464

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 08:21AM PST, ID: 20867563

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 08:34AM PST, ID: 20867670

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 08:50AM PST, ID: 20867802

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 08:59AM PST, ID: 20867899

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 09:02AM PST, ID: 20867931

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 09:10AM PST, ID: 20868024

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 11:18AM PST, ID: 20869228

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 11:30AM PST, ID: 20869326

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 11:33AM PST, ID: 20869360

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 11:34AM PST, ID: 20869377

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.11.2008 at 11:37AM PST, ID: 20869418

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.11.2008 at 08:11AM PST, ID: 20867464

Rank: Genius

You can bind the TreeView to anything that inherits from HierarchicalDataSourceControl.  Currently in 2.0 that is the SiteMapDataSource and the XmlDataSource.

Bob
 
02.11.2008 at 08:21AM PST, ID: 20867563
Thanks Bob!

I'm not really familiar with the HierarchicalDataSourceControl, SiteMapDataSource or XmlDataSource.

Would you mind please elaborating on your previous comment?  Alternatively I could just try investigate these topics....

Thanks
 
02.11.2008 at 08:34AM PST, ID: 20867670

Rank: Genius

1) What is the datasource for the TreeView?

2) What kind of information are you showing in the TreeView?

3) Is this going to be used for information display or site navigation?

Bob
 
02.11.2008 at 08:50AM PST, ID: 20867802
Bob,

>>>>>>>>1) What is the datasource for the TreeView?
It will be a Datatable or Dataview.  The structure of which will look like this:
StructID - auto increment number used as primary key
StructCode - Varchar.  Code to represent position in hierarchy.  Example: 01 & 01-01 & 01-02.
StructDesc - Varchar.  Name of the record.  This will become the text of the treeview node.
StructLinkID - Varchar.  Contains the StructCode of the parentnode for this record.

>>>>>>>2) What kind of information are you showing in the TreeView?
Not sure what you mean...   But the hierarchy will represent a folder structure (taxonomy) of a Microsoft SharePoint site.  My client has a standard folder structure for its sharepoint sites which is stored in a SQL Server table as detailed in question 1.  My application edits the master copy of this structure.  Before editing may begin, the whole structure must be displayed in a tree view.  The user will typically right-click on a node and have a context menu that will allow for an option to add a sub-folder.  Hope this helps???


>>>>>>>>3) Is this going to be used for information display or site navigation?
This is a desktop app so I imagine the answer could only be "display information only".


Does this help?  If you require any other information please let me know.
Thanks
 
02.11.2008 at 08:59AM PST, ID: 20867899

Rank: Genius

>>This is a desktop app
Disregard what I said, since that applies to ASP.NET, which why you wouldn't have heard of SiteMapDataSource or XmlDataSource.

With WinForms, it is significantly different.  

Proof-of-concept article:

Data Binding TreeView in C#
http://www.codeproject.com/KB/tree/dbTree.aspx

Bob
 
02.11.2008 at 09:02AM PST, ID: 20867931
Sorry Bob!!!

Should have mentioned it's a desktop app.

Please accept my apologies.

I'm having a look at the article right now.
Thanks
 
02.11.2008 at 09:10AM PST, ID: 20868024

Rank: Genius

No problem, I always think ASP.NET before WinForms, and I didn't ask!!

Bob
 
02.11.2008 at 11:18AM PST, ID: 20869228
Bob,

I've got the same habit, only with WinForms ....

I've run into a problem.  

From the article I got:

    this.myDataView.Sort = pub_id, au_id, title_id;
    this.myTree.AddGroup ("Publisher", "pub_id", "pub_name", "pub_id", 0, 0);
    this.myTree.AddGroup ("Author", "au_id", "au_name", "au_id", 1, 3);

The problem is that my treeview control does not have an "AddGroup" member.  The article is based on dot net 1.0.  I'm using 2.0.  I looked for a similar member but couldn't find anything.

You have any suggestions?

Thanks
 
 
02.11.2008 at 11:30AM PST, ID: 20869326

Rank: Genius

Here is updated source code for 2.0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
' This is the C# implementation of Data Binding PictureBox converted from 
' VB .NET by LZF of www.codeproject.com. The original code and article, 
' writen by Duncan Mackenzie of MSDN, can be found at 
' "http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/custcntrlsamp3.asp" 
' 
' Following block is the copyright and the description from the original code 
 
'Copyright (C) 2002 Microsoft Corporation 
'All rights reserved. 
 
'THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER 
'EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
'MERCHANTIBILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. 
 
'Date: May 2002 
'Author: Duncan Mackenzie 
 
'Requires the release version of .NET Framework 
 
'This control allows you to provide a file path 
'to its ImagePath property and have it load the image 
 
'Having this ImagePath property allows the control 
'to be databound, therefore supporting the display 
'of a picture based on a path stored in a database. 
 
'thinking that missing files and bad images would be 
'common, I am handling those errors simply by 
'clearing the image. That way you won't get an exception 
'but you also won't see the last record's image 
'with the current record's data. 
 
'If you would rather have an exception occur, 
'simply rewrite the ImagePath property and the 
'UpdateImage functions to remove the Exists check 
'and the Try Catch... like this; 
 
'Public Property ImagePath() As String 
' Get 
' Return m_ImagePath 
' End Get 
' Set(ByVal Value As String) 
' If Value <> m_ImagePath Then 
' m_ImagePath = Value 
' UpdateImage() 
' RaiseEvent ImagePathChanged(CObj(Me), New EventArgs()) 
' End If 
' End Set 
'End Property 
 
'Private Sub UpdateImage() 
' Me.Image = New Bitmap(ImagePath) 
'End Sub 
 
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Windows.Forms
 
Namespace ControlLib
 
  Public Class dbImageBox
    Inherits System.Windows.Forms.PictureBox
    Private m_ImagePath As String
 
    Protected Overloads Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
      ' TODO: Add custom paint code here 
 
      ' Calling the base class OnPaint 
      MyBase.OnPaint(pe)
    End Sub
 
    Public Property ImagePath() As String
      Get
        Return Me.m_ImagePath
      End Get
      Set(ByVal value As String)
        If value <> Me.m_ImagePath Then
          Me.m_ImagePath = value
          If System.IO.File.Exists(value) Then
            UpdateImage()
          Else
            Me.Image = Nothing
          End If
        End If
      End Set
    End Property
 
    Public Sub UpdateImage()
      Try
        Me.Image = Image.FromFile(ImagePath)
      Catch e As System.Exception
        MessageBox.Show(e.Message)
        Me.Image = Nothing
      End Try
    End Sub
 
  End Class
End Namespace
Open in New Window
Assisted Solution
 
02.11.2008 at 11:33AM PST, ID: 20869360

Rank: Genius

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
515:
516:
517:
518:
519:
520:
521:
522:
523:
524:
525:
526:
527:
528:
529:
530:
531:
532:
533:
534:
535:
536:
537:
' This is the C# implementation of Data Binding TreeView Control converted and modified 
' from VB .NET by LZF of www.codeproject.com. The original code and article, 
' writen by Duncan Mackenzie of MSDN, can be found at 
' "http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/custcntrlsamp3.asp" 
' 
' Following lines are the copyright from the original code 
 
' 
' * Copyright (C) 2002 Microsoft Corporation 
' * All rights reserved. 
' * 
' * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER 
' * EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
' * MERCHANTIBILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. 
' * 
' * Date: May 2002 
' * Author: Duncan Mackenzie 
' * 
' * Requires the release version of .NET Framework 
' 
 
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Windows.Forms
 
Namespace ControlLib
  Public Class dbTreeViewCtrl
    Inherits System.Windows.Forms.TreeView
 
    Protected Overloads Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
      ' TODO: Add custom paint code here 
 
      ' Calling the base class OnPaint 
      MyBase.OnPaint(pe)
    End Sub
 
    Private m_autoBuild As Boolean = True
 
    Public Property AutoBuildTree() As Boolean
      Get
        Return Me.m_autoBuild
      End Get
      Set(ByVal value As Boolean)
        Me.m_autoBuild = value
      End Set
    End Property
 
#Region "Data Binding"
    Private m_currencyManager As CurrencyManager = Nothing
    Private m_ValueMember As String
    Private m_DisplayMember As String
    Private m_oDataSource As Object
 
    <Category("Data")> _
    Public Property DataSource() As Object
      Get
        Return m_oDataSource
      End Get
      Set(ByVal value As Object)
        If value Is Nothing Then
          Me.m_currencyManager = Nothing
          Me.Nodes.Clear()
        Else
          If Not (TypeOf value Is IList OrElse TypeOf m_oDataSource Is IListSource) Then
            Throw (New System.Exception("Invalid DataSource"))
          Else
            If TypeOf value Is IListSource Then
              Dim myListSource As IListSource = DirectCast(value, IListSource)
              If myListSource.ContainsListCollection = True Then
                Throw (New System.Exception("Invalid DataSource"))
              End If
            End If
            Me.m_oDataSource = value
            Me.m_currencyManager = DirectCast(Me.BindingContext(value), CurrencyManager)
            If Me.AutoBuildTree Then
              BuildTree()
            End If
          End If
        End If
      End Set
    End Property
    ' end of DataSource property 
    <Category("Data")> _
    Public Property ValueMember() As String
      Get
        Return Me.m_ValueMember
      End Get
      Set(ByVal value As String)
        Me.m_ValueMember = value
      End Set
    End Property
 
    <Category("Data")> _
    Public Property DisplayMember() As String
      Get
        Return Me.m_DisplayMember
      End Get
      Set(ByVal value As String)
        Me.m_DisplayMember = value
      End Set
    End Property
 
    Public Function GetValue(ByVal index As Integer) As Object
      Dim innerList As IList = Me.m_currencyManager.List
      If innerList IsNot Nothing Then
        If (Me.ValueMember <> "") AndAlso (index >= 0 AndAlso 0 < innerList.Count) Then
          Dim pdValueMember As PropertyDescriptor
          pdValueMember = Me.m_currencyManager.GetItemProperties()(Me.ValueMember)
          Return pdValueMember.GetValue(innerList(index))
        End If
      End If
      Return Nothing
    End Function
 
    Public Function GetDisplay(ByVal index As Integer) As Object
      Dim innerList As IList = Me.m_currencyManager.List
      If innerList IsNot Nothing Then
        If (Me.DisplayMember <> "") AndAlso (index >= 0 AndAlso 0 < innerList.Count) Then
          Dim pdDisplayMember As PropertyDescriptor
          pdDisplayMember = Me.m_currencyManager.GetItemProperties()(Me.ValueMember)
          Return pdDisplayMember.GetValue(innerList(index))
        End If
      End If
      Return Nothing
    End Function
 
#End Region
 
#Region "Building the Tree"
 
    Private treeGroups As New ArrayList()
 
    Public Sub BuildTree()
      Me.Nodes.Clear()
      If (Me.m_currencyManager IsNot Nothing) AndAlso (Me.m_currencyManager.List IsNot Nothing) Then
        Dim innerList As IList = Me.m_currencyManager.List
        Dim currNode As TreeNodeCollection = Me.Nodes
        Dim currGroupIndex As Integer = 0
        Dim currListIndex As Integer = 0
 
 
        If Me.treeGroups.Count > currGroupIndex Then
          Dim currGroup As Group = DirectCast(treeGroups(currGroupIndex), Group)
          Dim myFirstNode As dbTreeNode = Nothing
          Dim pdGroupBy As PropertyDescriptor
          Dim pdValue As PropertyDescriptor
          Dim pdDisplay As PropertyDescriptor
 
          pdGroupBy = Me.m_currencyManager.GetItemProperties()(currGroup.GroupBy)
          pdValue = Me.m_currencyManager.GetItemProperties()(currGroup.ValueMember)
          pdDisplay = Me.m_currencyManager.GetItemProperties()(currGroup.DisplayMember)
 
          Dim currGroupBy As String = Nothing
          If innerList.Count > currListIndex Then
            Dim currObject As Object
            While currListIndex < innerList.Count
              currObject = innerList(currListIndex)
              If pdGroupBy.GetValue(currObject).ToString() <> currGroupBy Then
                currGroupBy = pdGroupBy.GetValue(currObject).ToString()
 
                myFirstNode = New dbTreeNode(currGroup.Name, pdDisplay.GetValue(currObject).ToString(), currObject, pdValue.GetValue(innerList(currListIndex)), currGroup.ImageIndex, currGroup.SelectedImageIndex, _
                currListIndex)
 
                currNode.Add(DirectCast(myFirstNode, TreeNode))
              Else
                AddNodes(currGroupIndex, currListIndex, myFirstNode.Nodes, currGroup.GroupBy)
              End If
              ' end while 
            End While
            ' end if 
          End If
        Else
          ' end if 
          While currListIndex < innerList.Count
            AddNodes(currGroupIndex, currListIndex, Me.Nodes, "")
          End While
        End If
        ' end else 
        If Me.Nodes.Count > 0 Then
          Me.SelectedNode = Me.Nodes(0)
 
        End If
      End If
      ' end if 
    End Sub
 
    Private Sub AddNodes(ByVal currGroupIndex As Integer, ByRef currentListIndex As Integer, ByVal currNodes As TreeNodeCollection, ByVal prevGroupByField As String)
      Dim innerList As IList = Me.m_currencyManager.List
      Dim pdPrevGroupBy As System.ComponentModel.PropertyDescriptor = Nothing
      Dim prevGroupByValue As String = Nothing
 
 
      Dim currGroup As Group
 
      If prevGroupByField <> "" Then
        pdPrevGroupBy = Me.m_currencyManager.GetItemProperties()(prevGroupByField)
      End If
 
      currGroupIndex += 1
 
      If treeGroups.Count > currGroupIndex Then
        currGroup = DirectCast(treeGroups(currGroupIndex), Group)
        Dim pdGroupBy As PropertyDescriptor = Nothing
        Dim pdValue As PropertyDescriptor = Nothing
        Dim pdDisplay As PropertyDescriptor = Nothing
 
        pdGroupBy = Me.m_currencyManager.GetItemProperties()(currGroup.GroupBy)
        pdValue = Me.m_currencyManager.GetItemProperties()(currGroup.ValueMember)
        pdDisplay = Me.m_currencyManager.GetItemProperties()(currGroup.DisplayMember)
 
        Dim currGroupBy As String = Nothing
 
        If innerList.Count > currentListIndex Then
          If pdPrevGroupBy IsNot Nothing Then
            prevGroupByValue = pdPrevGroupBy.GetValue(innerList(currentListIndex)).ToString()
          End If
 
          Dim myFirstNode As dbTreeNode = Nothing
          Dim currObject As Object = Nothing
 
          While (currentListIndex < innerList.Count) AndAlso (pdPrevGroupBy IsNot Nothing) AndAlso (pdPrevGroupBy.GetValue(innerList(currentListIndex)).ToString() = prevGroupByValue)
            currObject = innerList(currentListIndex)
            If pdGroupBy.GetValue(currObject).ToString() <> currGroupBy Then
              currGroupBy = pdGroupBy.GetValue(currObject).ToString()
 
              myFirstNode = New dbTreeNode(currGroup.Name, pdDisplay.GetValue(currObject).ToString(), currObject, pdValue.GetValue(innerList(currentListIndex)), currGroup.ImageIndex, currGroup.SelectedImageIndex, _
              currentListIndex)
 
              currNodes.Add(DirectCast(myFirstNode, TreeNode))
            Else
              AddNodes(currGroupIndex, currentListIndex, myFirstNode.Nodes, currGroup.GroupBy)
            End If
          End While
        End If
      Else
        Dim myNewLeafNode As dbTreeNode
        Dim currObject As Object = Me.m_currencyManager.List(currentListIndex)
 
        If (Me.DisplayMember IsNot Nothing) AndAlso (Me.ValueMember IsNot Nothing) AndAlso (Me.DisplayMember <> "") AndAlso (Me.ValueMember <> "") Then
          Dim pdDisplayloc As PropertyDescriptor = Me.m_currencyManager.GetItemProperties()(Me.DisplayMember)
          Dim pdValueloc As PropertyDescriptor = Me.m_currencyManager.GetItemProperties()(Me.ValueMember)
 
          myNewLeafNode = New dbTreeNode(IIf(Me.Tag Is Nothing, "", Me.Tag.ToString()), pdDisplayloc.GetValue(currObject).ToString(), currObject, pdValueloc.GetValue(currObject), currentListIndex)
        Else
          myNewLeafNode = New dbTreeNode("", currentListIndex.ToString(), currObject, currObject, Me.ImageIndex, Me.SelectedImageIndex, _
          currentListIndex)
        End If
 
        currNodes.Add(DirectCast(myNewLeafNode, TreeNode))
        currentListIndex += 1
      End If
    End Sub
#End Region
 
#Region "Groups"
 
    Public Sub RemoveGroup(ByVal group As Group)
      If treeGroups.Contains(group) Then
        treeGroups.Remove(group)
        If Me.AutoBuildTree Then
          BuildTree()
        End If
      End If
    End Sub
 
    Public Sub RemoveGroup(ByVal groupName As String)
      For Each group As Group In Me.treeGroups
        If group.Name = groupName Then
          RemoveGroup(group)
          Return
        End If
      Next
    End Sub
 
    Public Sub RemoveAllGroups()
      Me.treeGroups.Clear()
      If Me.AutoBuildTree Then
        Me.BuildTree()
      End If
    End Sub
 
    Public Sub AddGroup(ByVal group As Group)
      Try
        treeGroups.Add(group)
        If Me.AutoBuildTree Then
          Me.BuildTree()
        End If
      Catch e As NotSupportedException
        MessageBox.Show(e.Message)
      Catch e As System.Exception
        Throw e
      End Try
    End Sub
 
    Public Sub AddGroup(ByVal name As String, ByVal groupBy As String, ByVal displayMember As String, ByVal valueMember As String, ByVal imageIndex As Integer, ByVal selectedImageIndex As Integer)
      Dim myNewGroup As New Group(name, groupBy, displayMember, valueMember, imageIndex, selectedImageIndex)
      Me.AddGroup(myNewGroup)
    End Sub
 
    Public Function GetGroups() As Group()
      Return DirectCast(treeGroups.ToArray(Type.[GetType]("Group")), Group())
    End Function
 
#End Region
 
    Public Sub SetLeafData(ByVal name As String, ByVal displayMember As String, ByVal valueMember As String, ByVal imageIndex As Integer, ByVal selectedImageIndex As Integer)
      Me.Tag = name
      Me.DisplayMember = displayMember
      Me.ValueMember = valueMember
      Me.ImageIndex = imageIndex
      Me.SelectedImageIndex = selectedImageIndex
    End Sub
 
    Public Function IsLeafNode(ByVal node As TreeNode) As Boolean
      Return (node.Nodes.Count = 0)
    End Function
 
#Region "Keeping Everything In Sync"
 
    Public Function FindNodeByValue(ByVal value As Object) As TreeNode
      Return FindNodeByValue(value, Me.Nodes)
    End Function
 
    Public Function FindNodeByValue(ByVal Value As Object, ByVal nodesToSearch As TreeNodeCollection) As TreeNode
      Dim i As Integer = 0
      Dim currNode As TreeNode
      Dim leafNode As dbTreeNode
 
      While i < nodesToSearch.Count
        currNode = nodesToSearch(i)
        i += 1
        If currNode.LastNode Is Nothing Then
          leafNode = DirectCast(currNode, dbTreeNode)
          If leafNode.Value.ToString() = Value.ToString() Then
            Return currNode
          End If
        Else
          currNode = FindNodeByValue(Value, currNode.Nodes)
          If currNode IsNot Nothing Then
            Return currNode
          End If
        End If
      End While
 
      Return Nothing
    End Function
 
    Private Function FindNodeByPosition(ByVal posIndex As Integer) As TreeNode
      Return FindNodeByPosition(posIndex, Me.Nodes)
    End Function
 
    Private Function FindNodeByPosition(ByVal posIndex As Integer, ByVal nodesToSearch As TreeNodeCollection) As TreeNode
      Dim i As Integer = 0
      Dim currNode As TreeNode
      Dim leafNode As dbTreeNode
 
      While i < nodesToSearch.Count
        currNode = nodesToSearch(i)
        i += 1
        If currNode.Nodes.Count = 0 Then
          leafNode = DirectCast(currNode, dbTreeNode)
          If leafNode.Position = posIndex Then
            Return currNode
          Else
            currNode = FindNodeByPosition(posIndex, currNode.Nodes)
            If currNode IsNot Nothing Then
              Return currNode
            End If
          End If
        End If
      End While
      Return Nothing
    End Function
 
    Protected Overloads Overrides Sub OnAfterSelect(ByVal e As TreeViewEventArgs)
      Dim leafNode As dbTreeNode = DirectCast(e.Node, dbTreeNode)
 
      If leafNode IsNot Nothing Then
        If Me.m_currencyManager.Position <> leafNode.Position Then
          Me.m_currencyManager.Position = leafNode.Position
        End If
      End If
      ' TODO: Add MyTreeViewCtrl.OnAfterSelect implementation 
      MyBase.OnAfterSelect(e)
    End Sub
 
#End Region
 
  End Class
 
  Public Class Group
    Private groupName As String
    Private groupByMember As String
    Private groupByDisplayMember As String
    Private groupByValueMember As String
 
    Private groupImageIndex As Integer
    Private groupSelectedImageIndex As Integer
 
    Public Sub New(ByVal name As String, ByVal groupBy As String, ByVal displayMember As String, ByVal valueMember As String, ByVal imageIndex As Integer, ByVal selectedImageIndex As Integer)
      Me.ImageIndex = imageIndex
      Me.Name = name
      Me.GroupBy = groupBy
      Me.DisplayMember = displayMember
      Me.ValueMember = valueMember
      Me.SelectedImageIndex = selectedImageIndex
    End Sub
 
    Public Sub New(ByVal name As String, ByVal groupBy As String, ByVal displayMember As String, ByVal valueMember As String, ByVal imageIndex As Integer)
      Me.New(name, groupBy, displayMember, valueMember, imageIndex, imageIndex)
    End Sub
 
    Public Sub New(ByVal name As String, ByVal groupBy As String)
      Me.New(name, groupBy, groupBy, groupBy, -1, -1)
    End Sub
 
    Public Property SelectedImageIndex() As Integer
      Get
        Return groupSelectedImageIndex
      End Get
      Set(ByVal value As Integer)
        groupSelectedImageIndex = value
      End Set
    End Property
    Public Property ImageIndex() As Integer
      Get
        Return groupImageIndex
      End Get
      Set(ByVal value As Integer)
        groupImageIndex = value
      End Set
    End Property
 
    Public Property Name() As String
      Get
        Return groupName
      End Get
      Set(ByVal value As String)
        groupName = value
      End Set
    End Property
 
    Public Property GroupBy() As String
      Get
        Return groupByMember
      End Get
      Set(ByVal value As String)
        groupByMember = value
      End Set
    End Property
 
    Public Property DisplayMember() As String
      Get
        Return groupByDisplayMember
      End Get
      Set(ByVal value As String)
        groupByDisplayMember = value
      End Set
    End Property
 
    Public Property ValueMember() As String
      Get
        Return groupByValueMember
      End Get
      Set(ByVal value As String)
        groupByValueMember = value
      End Set
    End Property
  End Class
 
  Public Class dbTreeNode
    Inherits TreeNode
    Private m_groupName As String
    Private m_value As Object
    Private m_item As Object
    Private m_position As Integer
 
    Public Sub New()
    End Sub
 
    Public Sub New(ByVal groupName As String, ByVal text As String, ByVal item As Object, ByVal value As Object, ByVal imageIndex As Integer, ByVal selectedImgIndex As Integer, _
    ByVal position As Integer)
      Me.GroupName = groupName
      Me.Text = text
      Me.Item = item
      Me.Value = value
      Me.ImageIndex = imageIndex
      Me.SelectedImageIndex = selectedImgIndex
      Me.m_position = position
    End Sub
 
    Public Sub New(ByVal groupName As String, ByVal text As String, ByVal item As Object, ByVal value As Object, ByVal position As Integer)
      Me.GroupName = groupName
      Me.Text = text
      Me.Item = item
      Me.Value = value
      Me.m_position = position
    End Sub
 
    Public Property GroupName() As String
      Get
        Return m_groupName
      End Get
      Set(ByVal value As String)
        Me.m_groupName = value
      End Set
    End Property
 
    Public Property Item() As Object
      Get
        Return m_item
      End Get
      Set(ByVal value As Object)
        m_item = value
      End Set
    End Property
 
    Public Property Value() As Object
      Get
        Return m_value
      End Get
      Set(ByVal value As Object)
        m_value = value
      End Set
    End Property
 
    Public ReadOnly Property Position() As Integer
      Get
        Return m_position
      End Get
    End Property
  End Class
 
End Namespace
Open in New Window
Accepted Solution
 
02.11.2008 at 11:34AM PST, ID: 20869377

Rank: Genius

dbTreeViewCtrl has the AddGroup method.

Bob
 
02.11.2008 at 11:37AM PST, ID: 20869418
Thanks Bob

I'm trying again
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628