Populating the Menu - Navigation Control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I populate the menu control in 2.0 from a database. I know this can be
done with the Tree Control. Any code snippets? Thanks in advance.
 
Just check the following snippet of code and let me know whether this is
usefule to you

private void PopulateMenu()
{
DataSet ds = GetDataSetFromDatabase();
Menu menu = new Menu();

foreach (DataRow parentItem in ds.Tables["Categories"].Rows)
{
MenuItem categoryItem = new MenuItem((string)parentItem["CategoryName"]);
menu.Items.Add(categoryItem);

foreach (DataRow childItem in parentItem.GetChildRows("Children"))
{
MenuItem childrenItem = new MenuItem((string)childItem["ProductName"]);
categoryItem.ChildItems.Add(childrenItem);
}
}


}

private DataSet GetDataFromDatabase()
{
SqlConnection myConnection = new SqlConnection(GetConnectionString());
SqlDataAdapter adCat = new SqlDataAdapter("SELECT * FROM Categories",
myConnection);
SqlDataAdapter adProd = new SqlDataAdapter("SELECT * FROM Products",
myConnection);

DataSet ds = new DataSet();
adCat.Fill(ds, "Categories");
adProd.Fill(ds, "Products");
ds.Relations.Add("Children",
ds.Tables["Categories"].Columns["CategoryID"],
ds.Tables["Products"].Columns["CategoryID"]);
return ds;
}
 
Back
Top