Posted By

huangsb on 04/22/11


Tagged

WindowsForm


Versions (?)

Create ContextMenu from database


 / Published in: C#
 

  1. private void InitData(EOpenMode emOpenMode , string sDept)
  2. {
  3. //載入各評分細項右鍵功能表
  4. sql = "SELECT * FROM CallMonitorContextMenu";
  5. if (mis.DBOpenSQL(sql, ref rs)) {
  6. List<Label> categoryList = new List<Label>();
  7. categoryList.AddRange(
  8. new Label[] {
  9. label110, label120, label130, label140, label150, label160,
  10. label210, label220, label230, label240,
  11. label310, label320, label330, label340, label350, label360, label370}
  12. );
  13.  
  14. for (int i = 0; i < categoryList.Count; i++) {
  15. EnumerableRowCollection<DataRow> query = rs.AsEnumerable()
  16. .Where(row => row.Field<string>("Category") == categoryList[i].Text);
  17. ContextMenuStrip menu = new ContextMenuStrip();
  18. categoryList[i].ContextMenuStrip = menu;
  19. EnumerableRowCollection<DataRow> querySubMenuItem =
  20. query.Where(row => row.Field<int>("ParentMenuId") == 0);
  21. foreach (var row in querySubMenuItem) {
  22. ToolStripMenuItem subMenuItem =
  23. (ToolStripMenuItem)menu.Items.Add(row.Field<string>("MenuText"));
  24. CreateSubMenu(query, subMenuItem, row.Field<int>("MenuId"), 1);
  25. }
  26. }
  27. }
  28. }
  29.  
  30. private void CreateSubMenu(EnumerableRowCollection<DataRow> query,
  31. ToolStripMenuItem menu, int parentMenuId, int level) {
  32. if (level > 3)
  33. return;
  34. EnumerableRowCollection<DataRow> querySubMenuItem =
  35. query.Where(row => row.Field<int>("ParentMenuId") == parentMenuId);
  36. foreach (var row in querySubMenuItem) {
  37. ToolStripMenuItem subMenuItem = (ToolStripMenuItem)menu.DropDownItems.Add(row.Field<string>("MenuText"));
  38. subMenuItem.Tag = row.Field<string>("Category");
  39. subMenuItem.Click += new EventHandler(subMenuItem_Click);
  40. CreateSubMenu(query, subMenuItem, row.Field<int>("MenuId"), level + 1);
  41. }
  42. }

Report this snippet  

You need to login to post a comment.