Click here to Skip to main content
15,906,766 members
Home / Discussions / C#
   

C#

 
GeneralPrinter command codes with Crystal reports Pin
jdhale20-Feb-04 13:43
jdhale20-Feb-04 13:43 
Generalquestion about strings Pin
Rob Tomson20-Feb-04 13:29
Rob Tomson20-Feb-04 13:29 
GeneralRe: question about strings Pin
Heath Stewart20-Feb-04 13:42
protectorHeath Stewart20-Feb-04 13:42 
GeneralRe: question about strings Pin
Rob Tomson20-Feb-04 14:40
Rob Tomson20-Feb-04 14:40 
Questionhow to access web service with SSL from windows app (C#)? Pin
zhyluopro20-Feb-04 13:12
zhyluopro20-Feb-04 13:12 
AnswerRe: how to access web service with SSL from windows app (C#)? Pin
Heath Stewart20-Feb-04 13:21
protectorHeath Stewart20-Feb-04 13:21 
GeneralRecursive TreeView Pin
Ruchi Gupta20-Feb-04 13:08
Ruchi Gupta20-Feb-04 13:08 
GeneralRe: Recursive TreeView Pin
Heath Stewart20-Feb-04 13:39
protectorHeath Stewart20-Feb-04 13:39 
You need to make sure that a parent/child relationship exists in the DataSet, like:
ID  ParentID  Text
--- --------- -----
0   (null)    Node1
1   0         Node1_1
2   0         Node1_2
3   2         Node1_2_1
4   3         Node1_2_1_1
The first method of your function must get the root nodes by getting those with a ParentID that's null (DBNull):
public void BuildTree()
{
  DataSet ds = GetDataSet(); // Method to retrieve the DataSet - made-up as example
  DataRow[] rows = ds.Tables[0].Select("ParentID = NULL");
  treeView1.BeginUpdate(); // Don't update display till done == faster
  for (int i=0; i<rows.Length; i++)
  {
    TreeNode node = new TreeNode();
    node.Text = (string)rows[i]["Text"];
    node.Tag = rows[i]["ID"];
    this.treeView1.Nodes.Add(node);
    BuildSubTree(node, ds);
  }
  treeView1.EndUpdate();
}
In your actual recursive function, you could do something like this:
private void BuildSubTree(TreeNode parent, DataSet ds)
{
  int id = (int)parent.Tag;
  DataRow[] rows = ds.Tables[0].Select(string.Concat("ParentID = ", id.ToString()));
  for (int i=0; i<rows.Length; i++)
  {
    TreeNode node = new TreeNode();
    node.Text = (string)rows[i]["Text"];
    node.Tag = rows[i]["ID"];
    parent.Nodes.Add(node);
    BuildSubTree(node, ds);
  }
}
This is just a simpe example and can be greatly improved through the use of typed DataSet classes with defined DataRelations, which provide a faster way to get children using DataRow.GetChildRows. This should be enough to get you started.

 

Microsoft MVP, Visual C#
My Articles
GeneralListView Scroller Pin
Snowjim20-Feb-04 12:45
Snowjim20-Feb-04 12:45 
GeneralRe: ListView Scroller Pin
Heath Stewart20-Feb-04 13:23
protectorHeath Stewart20-Feb-04 13:23 
GeneralMultiple backbuffers DirectDraw Managed Pin
Jean Bédard20-Feb-04 11:16
Jean Bédard20-Feb-04 11:16 
GeneralRe: Multiple backbuffers DirectDraw Managed Pin
Heath Stewart20-Feb-04 11:33
protectorHeath Stewart20-Feb-04 11:33 
GeneralRe: Multiple backbuffers DirectDraw Managed Pin
Jean Bédard20-Feb-04 13:26
Jean Bédard20-Feb-04 13:26 
GeneralRe: Multiple backbuffers DirectDraw Managed Pin
SIDDHARTH_JAIN20-Feb-04 18:16
SIDDHARTH_JAIN20-Feb-04 18:16 
GeneralCollections Pin
gordingin20-Feb-04 8:44
gordingin20-Feb-04 8:44 
GeneralRe: Collections Pin
Heath Stewart20-Feb-04 10:08
protectorHeath Stewart20-Feb-04 10:08 
GeneralRe: Collections Pin
Guillermo Rivero20-Feb-04 10:11
Guillermo Rivero20-Feb-04 10:11 
GeneralRe: Collections Pin
Heath Stewart20-Feb-04 11:29
protectorHeath Stewart20-Feb-04 11:29 
GeneralRe: Collections Pin
Guillermo Rivero20-Feb-04 11:52
Guillermo Rivero20-Feb-04 11:52 
GeneralRe: Collections Pin
Heath Stewart20-Feb-04 13:18
protectorHeath Stewart20-Feb-04 13:18 
QuestionHow to make a Text Box only Numbers Pin
gr8tushar20-Feb-04 8:06
gr8tushar20-Feb-04 8:06 
AnswerRe: How to make a Text Box only Numbers Pin
Werdna20-Feb-04 8:31
Werdna20-Feb-04 8:31 
AnswerRe: How to make a Text Box only Numbers Pin
Judah Gabriel Himango20-Feb-04 8:34
sponsorJudah Gabriel Himango20-Feb-04 8:34 
GeneralParsers Pin
Husein20-Feb-04 7:40
Husein20-Feb-04 7:40 
GeneralRe: Parsers Pin
Werdna20-Feb-04 8:26
Werdna20-Feb-04 8:26 

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.