---
title: "MenuStrip with Nested Submenus"  
description: "MenuStrip with Nested SubmenusIn  my previous article I have discussed how to create a Database Driven Menu http://www.mindstick.com/Articles/26dc0999"  
author: "Shankar M"  
published: 2013-02-17  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/446/menustrip-with-nested-submenus  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# MenuStrip with Nested Submenus

## MenuStrip with Nested Submenus

In my previous article I have discussed how to create a [Database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) Driven Menu http://www.mindstick.com/Articles/26dc0999-6809-471b-93e4-74c7762d37a9/?Dynamic%20MenuStrip%20in%20Windows%20Forms in Windows Forms. In this blog, I have discussed how to create Nested Submenus, where I have altered the Table [Definition](https://yourviews.mindstick.com/view/70598/false-furore-over-the-leadership-definition-statement-of-army-chief-bipin-rawat) a little.

## Table Definitions:

## Parent Menu:

CREATE TABLE [dbo].[MNUPARENT](

[MENUPARVAL] [int] [IDENTITY](https://www.mindstick.com/articles/13090/icon-the-identity-of-your-brand)(1,1) NOT NULL PRIMARY KEY,

[MENUNAME] [varchar](20) NOT NULL,

[STATUS] [varchar](1));

## Sample Insert Statements:

INSERT INTO MNUPARENT(MENUNAME,STATUS)

VALUES('Offline','Y');

INSERT INTO MNUPARENT(MENUNAME,STATUS)

VALUES('Main Line','Y');

INSERT INTO MNUPARENT(MENUNAME,STATUS)

VALUES('Administration','Y');

## Child Menu:

CREATE TABLE [dbo].[MNUSUBMENU](

[MENUPARVAL] [int] NOT NULL,

[FRM_CODE] [varchar](50),

[FRM_NAME] [varchar](20) NOT NULL,

[MNUSUBMENU] [int] NOT NULL PRIMARY KEY,

[STATUS] [varchar](1))

## Sample Insert Statements:

INSERT INTO [MNUSUBMENU]([MENUPARVAL],[FRM_CODE],[FRM_NAME],[MNUSUBMENU],

[STATUS])VALUES(1,'FrmFinance','Finance',10,'Y')

INSERT INTO [MNUSUBMENU]([MENUPARVAL],[FRM_CODE],[FRM_NAME],[MNUSUBMENU],

[STATUS])VALUES(1,NULL,'[Accounting](https://www.mindstick.com/articles/13034/challenges-for-accounting-students)',20,'Y')

INSERT INTO [MNUSUBMENU]([MENUPARVAL],[FRM_CODE],[FRM_NAME],[MNUSUBMENU],

[STATUS])VALUES(20,NULL,'Audit',30,'Y')

INSERT INTO [MNUSUBMENU]([MENUPARVAL],[FRM_CODE],[FRM_NAME],[MNUSUBMENU],

[STATUS])VALUES(30,'FrmAccount','Acc. Standards',40,'Y')

## Updated Code Version

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

using System.Configuration;

using System.Reflection;

[namespace](https://www.mindstick.com/articles/12552/namespace) DynamicMenuStripDBDriven

{

public partial class FrmParent : Form

{

[SqlConnection](https://www.mindstick.com/forum/1209/sqlconnection-sqlcommand-sqldatareader-idisposable) conn;

MenuStrip MnuStrip;

ToolStripMenuItem MnuStripItem;

public FrmParent()

{

InitializeComponent();

}

private void FrmParent_Load(object sender, EventArgs e)

{

// To make this Form the Parent Form

this.IsMdiContainer = true;

//Creating object of MenuStrip class

MnuStrip = new MenuStrip();

String connectionString;

connectionString = [ConfigurationManager](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why).ConnectionStrings["dbconn"].ConnectionString;

conn = new SqlConnection(connectionString);

String Sequel = "SELECT MAINMNU,MENUPARVAL,STATUS FROM MNUPARENT";

[SqlDataAdapter](https://www.mindstick.com/interview/33991/what-is-sqldataadapter) da = new SqlDataAdapter(Sequel, conn);

[DataTable](https://www.mindstick.com/blog/195/datatable-in-ado-dot-net) dt = new DataTable();

conn.Open();

da.Fill(dt);

foreach (DataRow dr in dt.Rows)

{

MnuStripItem = new ToolStripMenuItem(dr["MAINMNU"].ToString());

SubMenu(MnuStripItem, dr["MENUPARVAL"].ToString());

MnuStrip.Items.Add(MnuStripItem);

}

// The Form.MainMenuStrip property determines the merge target.

this.MainMenuStrip = MnuStrip;

//Placing the control to the Form

this.Controls.Add(MnuStrip);

}

public void SubMenu(ToolStripMenuItem mnu, string submenu)

{

String Seqchild = "SELECT FRM_CODE,FRM_NAME,MNUSUBMENU FROM MNUSUBMENU WHERE MENUPARVAL='" + submenu + "'";

SqlDataAdapter dachildmnu = new SqlDataAdapter(Seqchild, conn);

DataTable dtchild = new DataTable();

dachildmnu.Fill(dtchild);

foreach (DataRow dr in dtchild.Rows)

{

ToolStripMenuItem SSMenu = new ToolStripMenuItem(dr["FRM_NAME"].ToString(), null, new EventHandler(ChildClick));

SubMenu(SSMenu, dr["MNUSUBMENU"].ToString());

mnu.DropDownItems.Add(SSMenu);

}

}

private void ChildClick(object sender, EventArgs e)

{

// MessageBox.Show(string.Concat("You have Clicked ", sender.ToString(), " Menu"), "Menu Items Event",MessageBoxButtons.OK, MessageBoxIcon.Information);

String Seqtx = "SELECT FRM_CODE FROM MNUSUBMENU WHERE FRM_NAME='" + sender.ToString() + "'";

SqlDataAdapter datransaction = new SqlDataAdapter(Seqtx, conn);

DataTable dtransaction = new DataTable();

datransaction.Fill(dtransaction);

Assembly frmAssembly = Assembly.LoadFile([Application](https://www.mindstick.com/articles/12824/calculator-application-in-android).ExecutablePath);

foreach (Type type in frmAssembly.GetTypes())

{

//MessageBox.Show(type.Name);

if (type.BaseType == typeof(Form))

{

if (dtransaction.Rows.Count > 0)

{

if (type.Name == dtransaction.Rows[0][0].ToString())

{

Form frmShow = (Form)frmAssembly.CreateInstance(type.ToString());

// then when you want to close all of them simple call the below code

foreach (Form form in this.MdiChildren)

{

form.Close();

}

frmShow.MdiParent = this;

frmShow.WindowState = FormWindowState.Maximized;

//frmShow.ControlBox = false;

frmShow.Show();

}

}

}

}

}

}

}

Thanks for reading.

If you have any queries, Please feel free to post your comments and doubts.

---

Original Source: https://www.mindstick.com/blog/446/menustrip-with-nested-submenus

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
