Click here to Skip to main content
15,867,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
I have created the menu in c# from whose menuitems are brought database.

My problem is how can I create the sub menus belongs to the menu under mainmenu?

eg. my data base fields are.....

tx_code tx_name type seq_no main_func sub_menu
A A T 1 ACCOUNTS
A1 A1 T 2 ACCOUNTS A
A2 A2 T 3 ACCOUNTS A
P P T 1 PARTS
P1 P1 T 2 PARTS P
P2 P2 T 3 PARTS P
NULL NULL NULL NULL NULL NULL

Please tell me how can I create the sub menu for the menu A.
The sub menus of Menu A is A1 and A2.

My code for that is ....
C#
ds = clsdb.getDataset("SP_MENU");
   int j = ds.Tables[0].Rows.Count;
   MenuItem[] mi = new MenuItem[j];
   for (int i = 0; i < j; i++)
   {
       mi[i] = new MenuItem();
       mi[i].Index =int.Parse(ds.Tables[0].Rows[i]["seq_no"].ToString ());
       mi[i].Text = ds.Tables[0].Rows[i]["tx_name"].ToString();
       mi[i].MergeType = MenuMerge.MergeItems;
   }


   MainMenu MM = new MainMenu();
   MM.MenuItems.Add("Accounts", mi);

   this.Menu = MM;


Thanks
Posted
Updated 13-Sep-10 21:21pm
v2
Comments
Dalek Dave 14-Sep-10 3:21am    
Edited for Grammar and Code Block.
Abhishek Sur 14-Sep-10 4:46am    
MenuItems does not allow DataBinding. So Loop is the only option to create menu.

Hi Sagar55,

Add Sub Menu[^]

I hope this link helpful to you.

Cheers :)
 
Share this answer
 
Comments
Sagar Khairnar 55 14-Sep-10 2:29am    
Sorry,
is there any another way to create submenu from dataset i.e from database table...
Hi,

// Its for Main Menu Adding

for(int ix=0;ix< dt.Rows.Count;ix++)
{
  MenuItem main= new MenuItem();
  main.Text = dt.Rows[ix]["Name"].ToString();
  menu.MenuItems.Add(main);
  CallSubMenu("Name",main);
}

// Its for subMenu Adding
private void CallSubMenu(string sName,MenuItem main)
{ 
 //Pass sName into main DataTable to filter its related data's.
  for(int ix=0;ix< dtSub.Rows.Count;ix++)
 {
  MenuItem submenu= new MenuItem();
  submenu.Text = dtSub.Rows[ix]["Name"].ToString();
  main.MenuItems.Add(submenu);
 }
}


Just refer this code for your idea.You can change this for what do you need.

Cheers :)
 
Share this answer
 
Comments
Sagar Khairnar 55 14-Sep-10 4:44am    
Thank U Sir,
ur code works but i have problem..
As per ur suggestion i did the code but, it does not adding my submenu. also not displaying any error...
pls inform the necessary changes
my code is given below

private void button1_Click(object sender, EventArgs e)
{

ds = clsdb.getDataset("SP_MENU");
int j = ds.Tables[0].Rows.Count;
MenuItem[] mi = new MenuItem[j];

for (int i = 0; i < j; i++)
{
mi[i] = new MenuItem();
mi[i].Text = ds.Tables[0].Rows[i]["tx_name"].ToString();
mi[i].MergeType = MenuMerge.MergeItems;
a = ds.Tables[0].Rows[i]["sub_menu"].ToString().Trim();
if (a == "")
{
MM.MenuItems.Add(mi[i].Text);
CallSubMenu(ds.Tables[0].Rows[i]["tx_code"].ToString(), mi[i]);
}
}
this.Menu = MM;
}



// Its for subMenu Adding
private void CallSubMenu(string sName, MenuItem main)
{
SqlParameter[] par = new SqlParameter[1];
par[0] = new SqlParameter("@sub_menu", SqlDbType.VarChar);
par[0].Value = sName;
dssub = clsdb.getDataset("SP_SUBMENU",par);

int p=dssub.Tables[0].Rows.Count ;

MenuItem[] submenu = new MenuItem[p];
for (int ix = 0; ix < p; ix++)
{
submenu[ix] = new MenuItem();
submenu[ix].Text = dssub.Tables[0].Rows[ix]["tx_name"].ToString();
submenu[ix].MergeType = MenuMerge.MergeItems;
main.MenuItems.Add(submenu[ix].Text);
}
this.Menu = MM;
}
}
Try this and it really works. I have used in my earlier project.

http://aspalliance.com/822[^]
 
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