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 this code.
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");
col = new DataColumn("Info");
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["ID"] = "1";
r["PID"] = "0";
r["Info"] = "Menu1";
r["ID"] = "10";
r["Info"] = "Menu10";
r["ID"] = "2";
r["Info"] = "Menu2";
r["ID"] = "3";
r["Info"] = "Menu3";
r["ID"] = "4";
r["PID"] = "1";
r["Info"] = "Menu4";
r["ID"] = "5";
r["PID"] = "4";
r["Info"] = "Menu5";
r["ID"] = "6";
r["PID"] = "5";
r["Info"] = "Menu6";
r["ID"] = "7";
r["PID"] = "2";
r["Info"] = "Menu7";
r["ID"] = "11";
r["PID"] = "6";
r["Info"] = "Menu11";
r["ID"] = "8";
r["PID"] = "10";
r["Info"] = "Menu8";
r["ID"] = "9";
r["PID"] = "3";
r["Info"] = "Menu9";
r["ID"] = "12";
r["PID"] = "7";
r["Info"] = "Menu12";
r["ID"] = "13";
r["Info"] = "Menu13";
#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();
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);
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 "";