I have filled menustrip from database mainmenu and sub menu, on form load it is showing main menu and sub menu but i want on click of submenu to open form.

Sep 25 2012 4:37 AM

this is my code.

 public partial class Form1 : Form
    {
        string strcon = @"Data Source= SV3\SQLEXPRESS;Initial Catalog= AarambhStandalone;Integrated Security= true";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(strcon);
            con.Open();
            SqlCommand cmd = new SqlCommand("select Description from tblMenu", con);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);

            MenuStrip mnu = new MenuStrip();
            if (ds.Tables[0].Rows.Count != 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    mnu.Items.Add(ds.Tables[0].Rows[i]["Description"].ToString(), null, new EventHandler(MenuItemClicked));
                }

                this.Controls.Add(mnu);
            }
        }

        private void MenuItemClicked(object sender, EventArgs e)
        {
            ContextMenuStrip cntx = new ContextMenuStrip();
            List<string> lstMenu = new List<string>();
            SqlConnection con = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter dr = new SqlDataAdapter();
            int mnuID = 0;
            string strSelect = "Select mnuID from tblMenu where Description='" + sender.ToString() + "'";
            cmd = new SqlCommand(strSelect, con);
            con.Open();
            dr.SelectCommand = cmd;
            DataSet ds = new DataSet();
            DataSet ds1 = new DataSet();
            dr.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                mnuID = Convert.ToInt16(ds.Tables[0].Rows[0]["mnuID"].ToString());
            }

            strSelect = "select MenuText from tblsubmenu where mnuID='" + mnuID + "'";
            cmd = new SqlCommand(strSelect, con);
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }

            dr.SelectCommand = cmd;
            dr.Fill(ds1);
            if (ds1.Tables[0].Rows.Count != 0)
            {
                for (int icnt = 0; icnt < ds1.Tables[0].Rows.Count; icnt++)
                {
                    lstMenu.Add(ds1.Tables[0].Rows[icnt]["MenuText"].ToString());
                }
            }
            foreach (string str in lstMenu)
            {
                cntx.Items.Add(str);
            }
            ToolStripMenuItem tsi = (ToolStripMenuItem)sender;
            tsi.DropDown = cntx;
          
        }

 }






on click of shift definition on want to open shiftdefinition form.
on click of shift definition on want to open Regularization form.

data filled in menustrip from database



Answers (1)