473,326 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Why does this code work?


I am new to C# and have been studying this piece of code. It loops
through an Adjacency Matrix table to populate a tree view. I have two
questions about why this code works.

1. initTreeView(TreeNode N) creates a new "temp" table to hold
the children for each node. Each time the table is created it has the
same name "temp". Why doesn't the table just get over written each
time?
2. In the foreach (DataRow r3 in temp.Rows) loop, if the temp
table is empty like when Menu11 is reached the call to
initTreeView(tn) is not executed but for all the parent nodes Menu1,
Menu4, Menu5 and Menu6 initTreeView(tn) is executed. There is no code
to indicate that at Menu11 not to execute initTreeView(tn) How does
C# know when to execute initTreeView(tn) and when not to?
namespace DynamicTree
{
public partial class Form1 : Form
{
//Declare the table that will be populated with the Adjacency
Matrix data.
DataTable tbl;
//Declare the coumns that will be populated in the Adjacency
Matrix table.
DataColumn col;

public Form1()
{
InitializeComponent();
//Create the Adjacency Matrix table.
InitTable();
//Populate the Adjacency Matrix table.
initTableData();
}

#region InitTable() - Create the Adjacency Matrix table.

private void InitTable()
{//Create the Adjacency Matrix table.

tbl = new DataTable();

col = new DataColumn("ID");
tbl.Columns.Add(col);
col = new DataColumn("PID");
tbl.Columns.Add(col);
col = new DataColumn("Info");
tbl.Columns.Add(col);

tbl.AcceptChanges();

}

#endregion InitTable() - Create the Adjacency Matrix table.

#region initTableData() - Populate the Adjacency Matrix table.

private void initTableData()
{//Populate the Adjacency Matrix table.
DataRow r;

r = tbl.NewRow();
r["ID"] = "0";
r["PID"] = "-1";
r["Info"] = "Root";
tbl.Rows.Add(r);
r = tbl.NewRow();
r["ID"] = "1";
r["PID"] = "0";
r["Info"] = "Menu1";
tbl.Rows.Add(r);

r = tbl.NewRow();
r["ID"] = "10";
r["PID"] = "0";
r["Info"] = "Menu10";
tbl.Rows.Add(r);

r = tbl.NewRow();
r["ID"] = "2";
r["PID"] = "0";
r["Info"] = "Menu2";
tbl.Rows.Add(r);

r = tbl.NewRow();
r["ID"] = "3";
r["PID"] = "0";
r["Info"] = "Menu3";
tbl.Rows.Add(r);

r = tbl.NewRow();
r["ID"] = "4";
r["PID"] = "1";
r["Info"] = "Menu4";
tbl.Rows.Add(r);

r = tbl.NewRow();
r["ID"] = "5";
r["PID"] = "4";
r["Info"] = "Menu5";
tbl.Rows.Add(r);

r = tbl.NewRow();
r["ID"] = "6";
r["PID"] = "5";
r["Info"] = "Menu6";
tbl.Rows.Add(r);

r = tbl.NewRow();
r["ID"] = "7";
r["PID"] = "2";
r["Info"] = "Menu7";
tbl.Rows.Add(r);

r = tbl.NewRow();
r["ID"] = "11";
r["PID"] = "6";
r["Info"] = "Menu11";
tbl.Rows.Add(r);

r = tbl.NewRow();
r["ID"] = "8";
r["PID"] = "10";
r["Info"] = "Menu8";
tbl.Rows.Add(r);

r = tbl.NewRow();
r["ID"] = "9";
r["PID"] = "3";
r["Info"] = "Menu9";
tbl.Rows.Add(r);

r = tbl.NewRow();
r["ID"] = "12";
r["PID"] = "7";
r["Info"] = "Menu12";
tbl.Rows.Add(r);

r = tbl.NewRow();
r["ID"] = "13";
r["PID"] = "4";
r["Info"] = "Menu13";
tbl.Rows.Add(r);

}

#endregion initTableData() - Populate the Adjacency Matrix
table.

#region Form1_Load() - Create the root tree node.

private void Form1_Load(object sender, EventArgs e)
{
//Create the root tree node.
TreeNode r = new TreeNode();
r.Text = "Root";
initTreeView(r);
tree.Nodes.Add(r);
tree.ExpandAll();
}

#endregion Form1_Load()

//The first pass the root tree node created in ther form load
is passed.
private void initTreeView(TreeNode N)
{//This is the recursive method, calling it's self from the
FOR loop at 201.
//This creates nested tables containing the children for each
node.

//Create a temp table to act as a buffer to hold the
//children of the current node.
DataTable temp = new DataTable();
col = new DataColumn("ID");
temp.Columns.Add(col);
col = new DataColumn("PID");
temp.Columns.Add(col);
col = new DataColumn("Info");
temp.Columns.Add(col);
temp.AcceptChanges();

//Retrieve the child ID of the current node.
string id = getID(N);
foreach (DataRow r1 in tbl.Rows)
{//Step through the Adjacency Matrix table to find
//all the children of the current node.

if (r1["PID"].ToString() == id)
{//This row represents a child of the current node.

//Add a row to the buffer table to contain this
child
//of the of the current node.
DataRow r2 = temp.NewRow();
r2["ID"] = r1["ID"].ToString();
r2["PID"] = r1["PID"].ToString();
r2["Info"] = r1["Info"].ToString();
temp.Rows.Add(r2);
temp.AcceptChanges();
}
}

foreach (DataRow r3 in temp.Rows)
{//Step through the buffer table and create a node for
each row.
//These are the children of the current node.

//If temp is empty control pops ouit and goes to
initTreeView()
//calls it's self, back to 157.

TreeNode tn = new TreeNode();
tn.Text = r3["Info"].ToString();

//This is where initTreeView() calls it's self, back
to 157
//To create a nested table to hold the children of
this node.
initTreeView(tn);

N.Nodes.Add(tn);
}
}

//Return the child ID of the current node.
private string getID(TreeNode N)
{

foreach (DataRow r in tbl.Rows)
{
//Step through the Adjacency Matrix table to find row
//representing the current node. Return the child ID.
if (r["Info"].ToString() == N.Text)
return r["ID"].ToString();
}
return "";
}
}
}
Oct 26 '07 #1
1 1180
xm******@yahoo.com wrote:
I am new to C# and have been studying this piece of code. It loops
through an Adjacency Matrix table to populate a tree view. I have two
questions about why this code works.
You should post this question in microsoft.public.dotnet.languages.csharp.
This newsgroup is for C++.

-cd
Oct 26 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As...
2
by: Paul THompson | last post by:
I have a piece of code something like the following: START OF CODE <html><head><title>The Wizard</title></head> <body> <h1>Welcome to Joe's Vet Clinic</h1> <div id="part1"...
162
by: Isaac Grover | last post by:
Hi everyone, Just out of curiosity I recently pointed one of my hand-typed pages at the W3 Validator, and my hand-typed code was just ripped to shreds. Then I pointed some major sites...
5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
4
by: rick | last post by:
The following basic script works fine in firefox by not in IE. Can anyone spot the problem? In IE I can only delete the first line but not the lines created by javascript. Also, look at the HTML...
6
by: benb | last post by:
I have form that looks a lot like a search bar for the user to search for records matching specified criteria (e.g. first names containing "ben"). For robust results, an intermediary form displays...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
14
by: webEater | last post by:
I have a problem, it's not browser specific, and I don't get a solution. I have an (X)HTML document, I show you a part of it: .... <!--<div class="pad">--> <div id="eventImages"><img src=""...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.