Click here to Skip to main content
16,012,352 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
foreach (DataRow dr in drowpar)
{
menuBar.Items.Add(new MenuItem(dr["Child"].ToString(), dr["Id"].ToString(), "", ""));

}
DataRow[] drowpar1 = dt.Select("[Parent] =" + 1);
foreach (DataRow dr in drowpar1)
{
menuBar.FindItem(dr1[Parent].ToString()).ChildItems.Add(mnu);
Child.Items.Add(new MenuItem(dr1["Child"].ToString(),"","" );
}



This is my Code...I want to add sub child for the Child items in the Menu control. I want a menu structure like this..Please help me to find it.

parant1 --->child1---->SubChild
--->child2
--->Child3

Parent2---->Child3
---->Child4
Posted
Updated 8-Apr-13 21:52pm
v2

1 solution

Try this:
Adding Parent Menu Items:
C#
private void AddTopMenuItems(DataTable menuData)
{
      DataView view = null;
      try
      {
          view = new DataView(menuData);
          view.RowFilter = "ParentID IS NULL";
          foreach (DataRowView row in view)
          {
               //Adding the menu item
               MenuItem newMenuItem = new MenuItem(row["Text"].ToString(), row["MenuID"].ToString());
               menuBar.Items.Add(newMenuItem);
               //Calling the function to add the child menu items
               AddChildMenuItems(menuData, newMenuItem); //This function will add child menu items to your menu control.
          }
      }
      catch (Exception ex)
      {
          //Show the error massage here
      }
      finally {
            view = null;
      }
}

Adding Child Menu Items:
C#
private void AddChildMenuItems(DataTable menuData, MenuItem parentMenuItem)
{
    DataView view = null;
    try
    {
        view = new DataView(menuData);
        view.RowFilter = "ParentID=" + parentMenuItem.Value;
        foreach (DataRowView row in view)
        {
            MenuItem newMenuItem = new MenuItem(row["Text"].ToString(), row["MenuID"].ToString());
            newMenuItem.NavigateUrl = row["NavigateUrl"].ToString();
            parentMenuItem.ChildItems.Add(newMenuItem);
            // This code is used to recursively add child menu items filtering by ParentID
            AddChildMenuItems(menuData, newMenuItem);
        }
    }
    catch (Exception ex)
    {
        //Show the error massage here
    }
    finally
    {
        view = null;
    }
}



--Amit
 
Share this answer
 
v2
Comments
Member 9492907 9-Apr-13 4:50am    
Its working!!!!!!!!!!!!!!! thank you soo much Amit
_Amy 9-Apr-13 4:50am    
Welcome. :)
Member 9492907 9-Apr-13 4:57am    
Once again Thank you soo much..i had try soo many codes , but only this gve me the result..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900