Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

i want to create a menu with sub menuitems... on windows form from my database table.....

i have a database table with the menu filed,submenu,index,menu name columns etc...

pls any one have any article or simple code(example) or any hint by which i can create the menu at runtime from database

thank u .
Posted

Hi,

Here is the sample code. try this,

MenuItem[] mi = new MenuItem[1];
mi[0] = new MenuItem();
mi[0].Index = 0;
mi[0].Text = "New";
mi[0].MergeType = MenuMerge.MergeItems;
          
MainMenu MM = new MainMenu();
MM.MenuItems.Add("File", mi);
                      
this.Menu = MM;


Regards,
Suresh
 
Share this answer
 
I assume you already know how to connect to the database and you already have a DataSet containing the data from the database table. Put the code below in the OnShown event of the form
DataTable dt = dataSet1.Tables[0];
MainMenu menu = new MainMenu();
List<menuitem> list = new List<menuitem>();

foreach(DataRow row in dt.Rows)
{
   MenuItem item  = new MenuItem();
   item.Text = row["menu_name"];
   list.Add(item);
}

menu.MenuItems.Add("Products", list.ToArray());
                      
this.Menu = MM;

</menuitem></menuitem>


I'm not very sure about the exact syntax, but with visual studio that shouldn't be a problem.
 
Share this answer
 

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