Introduction
When you build a application or website, you want to build a dynamic menu with many level depend on your categoris, sub-categories, products but you have never ever known exactly the number of level of your menu. Now, I make this one to show you the way to solve this problem.
Background
When you have to make a website of the shop. This shop have many product and they want to arrange their product to category, sub-category, product to many many level. They want to make it dynamic, they can make a new level (sub-sub-category,..) and you have to let them do that. When you develope, you don't know that how many level of menu could be builded.
To slove this problem, you should use resursive algorithm.
Here, I use TreeView in WinForm to demo this. I will read data in a Datatable to build a dynamic tree. This code can be used in ASP.NET. I use Visual Studio 2005 to develope it.
Using the code
First, I will define a DataTable to store the ID, PID(parent ID), Info of the menus...
DataTable tbl;
DataColumn col;
private void InitTable()
{
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();
}
Then, I will make the data for this table. This table is store the information of menus.
private void initTableData()
{
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);
}
Let's start to build the tree!
I make a method to build this tree. This tree has a "Root" with ID=0 and PID=-1. So that, any menu has PID=0 is a child of this menu and this "Root" with PID=-1 has no parent.
We will add this "Root" to the tree and call the method initTreeView(TreeNode N). We consider that all sub menu is a TreeNode and we will add this TreeNode to "Root". With another Node, we continue to consider it a "mini root" and another menu which are children of this node is a "mini node" so we will add to this "mini root" then, too.
private void initTreeView(TreeNode N)
{
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();
string id = getID(N);
foreach (DataRow r1 in tbl.Rows)
{
if (r1["PID"].ToString() == id)
{
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)
{
TreeNode tn = new TreeNode();
tn.Text = r3["Info"].ToString();
initTreeView(tn);
N.Nodes.Add(tn);
}
}
With any node, we will find all of its children (I define a new DataTable: temp to store those children), with each children will continue to call the method initTreeView() to build it. So on, so on, we will find the smallest menu and add it to its parent.
Points of Interest
With a short of code, we have builded a dynamic menu. It is simple to make, easy to remember and can be apply to WinForm or WebForm.
This algorithm could be use to any language.
Enjoy with my code! Thanks for reading!
Any problem or complain, please contact me at:
Name: Nguyen Anh Vu
Email: nguyenanhvu.aiti@gmail.com or spiderman_anhvu@yahoo.com