1
Reply

Dynamially Loading a Menu from Table based on the combo box Selection

Shankar M

Shankar M

Oct 29 2012 3:32 AM
1.3k

Hi Experts,

I am new to this Forum !!!!

I have a requirement where I need to dynamically load the Menu from Table, in C# Windows
Application based on the Language Selected from the Combo Box.


My Attempt:

Table Definition:

create table menu_table(
MAIN_MNU            varchar(50),
MAIN_MNU_GRP    int,
CHILD_MNU          varchar(50),
CHILD_MNU_GRP  int,
LANG                     varchar (50))
 

01private void loadMenu(string _language)
02        {   
03       string connectionString = "Data Source =...";
04        
05            string sql = "SELECT MAIN_MNU,MAIN_MNU_GRP,CHILD_MNU,CHILD_MNU_GRP FROM MENU_TABLE WHERE LANG='" + _language + "'";
06            SqlConnection connection = new SqlConnection(connectionString);
07            SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
08            DataSet ds = new DataSet();
09            connection.Open();
10            dataadapter.Fill(ds, "Menu Table");
11            connection.Close();
12 
13            MenuItem[] mnuItem = new MenuItem[ds.Tables[0].Rows.Count];
14            if (ds.Tables[0].Rows.Count > 0)
15            {
16                //ds.Tables[0].Rows.Count
17                for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
18                {
19                     string s = ds.Tables[0].Rows[j].ItemArray[0].ToString();
20                     mnuItem[j] = new MenuItem(s,new System.EventHandler(mnuClick));
21 
22                    if (ds.Tables[0].Rows[j].ItemArray[1].ToString() == "0")
23                    {                     
24                       this.mnuMainMenu.MenuItems.Add(ds.Tables[0].Rows[j].ItemArray[0].ToString());
25                    }
26                     if (ds.Tables[0].Rows[j].ItemArray[3].ToString() == "1")
27                    {
28                        int father = Convert.ToInt32(ds.Tables[0].Rows[j].ItemArray[1].ToString());
29                        //this.mnuMainMenu.MenuItems[father].MenuItems.Add(mnuItem[j]);
30                        this.mnuMainMenu.MenuItems[father].MenuItems.Add(ds.Tables[0].Rows[j].ItemArray[2].ToString());
31                    }
32                }
33                 
34                
35            }
36            this.Menu = mnuMainMenu;
37 
38        }
39 
40 private void mnuClick(object sender, System.EventArgs e)
41        {
42            MenuItem item = (MenuItem)sender;
43            string mess = item.Text.ToString();
44            MessageBox.Show("You clicked menu " + mess);
45 
46        }


The problem with this is,The event for the Button is not triggered when I press the Menu Item.
I can't able to Solve this. Kindly help me to solve this Issue.

Thanks & Regards,
Shanklar

Answers (1)