Return to Snippet

Revision: 45046
at April 22, 2011 19:22 by huangsb


Initial Code
private void InitData(EOpenMode emOpenMode , string sDept)
{
    //載入各評分細項右鍵功能表
    sql = "SELECT * FROM CallMonitorContextMenu";
    if (mis.DBOpenSQL(sql, ref rs)) {
        List<Label> categoryList = new List<Label>();
        categoryList.AddRange(
            new Label[] { 
			  label110, label120, label130, label140, label150, label160, 
			  label210, label220, label230, label240, 
			  label310, label320, label330, label340, label350, label360, label370}
			);

        for (int i = 0; i < categoryList.Count; i++) {
            EnumerableRowCollection<DataRow> query = rs.AsEnumerable()
			  .Where(row => row.Field<string>("Category") == categoryList[i].Text);
            ContextMenuStrip menu = new ContextMenuStrip();
            categoryList[i].ContextMenuStrip = menu;
            EnumerableRowCollection<DataRow> querySubMenuItem = 
			  query.Where(row => row.Field<int>("ParentMenuId") == 0);
            foreach (var row in querySubMenuItem) {
                ToolStripMenuItem subMenuItem = 
				  (ToolStripMenuItem)menu.Items.Add(row.Field<string>("MenuText"));
                CreateSubMenu(query, subMenuItem, row.Field<int>("MenuId"), 1);
            }
        }
    }
}

private void CreateSubMenu(EnumerableRowCollection<DataRow> query, 
  ToolStripMenuItem menu, int parentMenuId, int level) {
    if (level > 3)
        return;
    EnumerableRowCollection<DataRow> querySubMenuItem = 
	  query.Where(row => row.Field<int>("ParentMenuId") == parentMenuId);
    foreach (var row in querySubMenuItem) {
        ToolStripMenuItem subMenuItem = (ToolStripMenuItem)menu.DropDownItems.Add(row.Field<string>("MenuText"));
        subMenuItem.Tag = row.Field<string>("Category");
        subMenuItem.Click += new EventHandler(subMenuItem_Click);
        CreateSubMenu(query, subMenuItem, row.Field<int>("MenuId"), level + 1);
    }
}

Initial URL

                                

Initial Description

                                

Initial Title
Create ContextMenu from database

Initial Tags

                                

Initial Language
C#