Click here to Skip to main content
16,005,389 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to clear selected items from multiple checkedlistboxes Pin
Heath Stewart30-Aug-04 9:47
protectorHeath Stewart30-Aug-04 9:47 
GeneralRe: ISA bus access Pin
Dave Kreskowiak30-Aug-04 7:47
mveDave Kreskowiak30-Aug-04 7:47 
Generalget drive roots Pin
fire.fox30-Aug-04 7:36
fire.fox30-Aug-04 7:36 
GeneralRe: get drive roots Pin
Heath Stewart30-Aug-04 8:10
protectorHeath Stewart30-Aug-04 8:10 
GeneralRe: get drive roots Pin
leppie30-Aug-04 22:23
leppie30-Aug-04 22:23 
GeneralRe: get drive roots Pin
Heath Stewart31-Aug-04 6:15
protectorHeath Stewart31-Aug-04 6:15 
Generalpopulate treeview with dataset Pin
Anonymous30-Aug-04 7:01
Anonymous30-Aug-04 7:01 
GeneralRe: populate treeview with dataset Pin
Heath Stewart30-Aug-04 7:49
protectorHeath Stewart30-Aug-04 7:49 
Make sure that an enforced DataRelation or an assumed relationship exists in a DataTable within your DataSet, like ParentID -> ID. For the top-level rows, ParentID (in this example) should be NULL.

To begin, find the rows with a NULL ParentID using DataTable.Select('ParentID = NULL'). That gives you a DataRow[] array. For each DataRow you add a TreeNode to TreeView.Nodes and then pass DataRow.GetChildRows using a DataRelation (best way of doing it), or dynamically construct another WHERE clause to pass to DataTable.Select like I did above (except replacing NULL with the current row's ID).

In a recursive method you add a TreeNode for each row and repeat the steps above, passing them to the same method. This is recursion.

The way you're doing it is not recursive and will only work for a level or two. Besides, you shouldn't do this in the Form.Load event handler. This can be a very expensive call and the form will be delayed. Users expact fast results and will think your application has hung and will most likely try to kill it or just stop using it. Consider starting a new thread (and be sure to use Control.Invoke) that does this so the form can load, optionally with user feedback like a ProgressBar while processing results.

Take a look at http://www.codeproject.com/script/comments/forums.asp?msg=745216&forumid=1649#xx745234xx[^] where I've explained this before in this forum.

Below is something similar to a method I used before to add TreeNodes to a TreeView safely. You should read the documentation for Control.Invoke in the .NET Framework SDK for more details:
private delegate int AddTreeNodeCallback(TreeNode node);
private void SafeAddTreeNode(TreeNodeCollection nodes, TreeNode node)
{
  if (treeView1.InvokeRequired)
  {
    AddTreeNodeCallback cb = new AddTreeNodeCallback(nodes.Add);
    treeView1.Invoke(cb, new object[] {node});
  }
  else nodes.Add(node);
}
I really do recommend starting this in another thread and using this method to add the TreeNode. Since you can easily get the row count from your DataTable, you could easily update a ProgressBar, but you should use Control.Invoke still. Always use Control.Invoke when communicating with a control created on a different thread.

Finally, try to avoid refering to DataColumns and DataRelations by their string name. This is very expensive as many lookups are required. Use either an integer index or the actual DataColumn or DataRelation object, which is trivial when using a typed DataSet (from using xsd.exe or VS.NET to create a new DataSet schema, which is available when you right-click on a project and select Add->Add New Item->DataSet). You can also programmatically create one and refer to the two classes I mentioned above that you programmatically added to the named DataTable, which you add to a named DataSet. This results in much faster code.

This posting is provided "AS IS" with no warranties, and confers no rights.

Software Design Engineer
Developer Division Sustained Engineering
Microsoft

[My Articles]
QuestionCan apple ArrayList in string[] Pin
arbrsoft30-Aug-04 6:57
arbrsoft30-Aug-04 6:57 
AnswerRe: Can apple ArrayList in string[] Pin
Judah Gabriel Himango30-Aug-04 7:02
sponsorJudah Gabriel Himango30-Aug-04 7:02 
GeneralNested UserControls Pin
Den2Fly30-Aug-04 6:16
Den2Fly30-Aug-04 6:16 
GeneralRe: Nested UserControls Pin
Charlie Williams30-Aug-04 6:24
Charlie Williams30-Aug-04 6:24 
GeneralRe: Nested UserControls Pin
Nick Parker30-Aug-04 6:25
protectorNick Parker30-Aug-04 6:25 
QuestionHas anyone attempted synchronized scolling? Pin
LongRange.Shooter30-Aug-04 6:12
LongRange.Shooter30-Aug-04 6:12 
AnswerRe: Has anyone attempted synchronized scolling? Pin
Heath Stewart30-Aug-04 7:34
protectorHeath Stewart30-Aug-04 7:34 
GeneralRe: Has anyone attempted synchronized scolling? Pin
LongRange.Shooter31-Aug-04 6:50
LongRange.Shooter31-Aug-04 6:50 
GeneralRe: Has anyone attempted synchronized scolling? Pin
progload3-Oct-04 10:21
progload3-Oct-04 10:21 
GeneralErrorProvider() Not Displayed Pin
dbetting30-Aug-04 5:36
dbetting30-Aug-04 5:36 
Generalinstalling global cbt hooks in c# Pin
vignesh_s30-Aug-04 3:51
vignesh_s30-Aug-04 3:51 
GeneralRe: installing global cbt hooks in c# Pin
Nick Parker30-Aug-04 4:15
protectorNick Parker30-Aug-04 4:15 
GeneralRe: installing global cbt hooks in c# Pin
Duncan Edwards Jones30-Aug-04 4:15
professionalDuncan Edwards Jones30-Aug-04 4:15 
GeneralRe: installing global cbt hooks in c# Pin
vignesh_s3-Sep-04 3:40
vignesh_s3-Sep-04 3:40 
Generalset time in c# Pin
erina54830-Aug-04 3:14
erina54830-Aug-04 3:14 
GeneralRe: set time in c# Pin
Nick Parker30-Aug-04 4:27
protectorNick Parker30-Aug-04 4:27 
GeneralRe: set time in c# Pin
Heath Stewart30-Aug-04 5:56
protectorHeath Stewart30-Aug-04 5:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.