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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top