Beispiel #1
0
      private void button1_Click(object sender, EventArgs e) {
         if(textBox1.Text == String.Empty) {
            MessageBox.Show("Please, fill all fields");
            return;
         }

         try {
            var objFirstGroup = Form1.objModel.Groups.OrderBy(c => c.Id).FirstOrDefault();

            //if no one nodes in base
            if(objFirstGroup == null) {
               objFirstGroup = new Group() {
                  Name = textBox1.Text,
                  LeftKey = 1,
                  RightKey = 2,
                  Level = 0
               };
               Form1.objModel.Groups.Add(objFirstGroup);
               Form1.objModel.SaveChanges();
            }
            else {
               var objParentNode = Form1.objModel.Groups.Where(gr => gr.Id == iNodeId).Single();
               //Обновляем ключи существующего дерева, узлы стоящие за родительским узлом:
               //UPDATE my_tree SET left_key = left_key + 2, right_ key = right_ key + 2 WHERE left_key > $right_ key
               var objResultList = Form1.objModel.Groups.Where(group => group.LeftKey > objParentNode.RightKey).
                  ToList();

               foreach(var gr in objResultList) {
                  gr.LeftKey += 2;
                  gr.RightKey += 2;
               }
               Form1.objModel.SaveChanges();

               //Обновляем родительскую ветку:
               //UPDATE my_tree SET right_key = right_key + 2 WHERE right_key >= $right_key AND left_key < $right_key
               objResultList = Form1.objModel.Groups.Where(group => group.RightKey >= objParentNode.RightKey && group.LeftKey < objParentNode.RightKey).ToList();
               foreach(var gr in objResultList) {
                  gr.RightKey += 2;
               }
               Form1.objModel.SaveChanges();

               //Теперь добавляем новый узел :
               //INSERT INTO my_tree SET left_key = $right_key, right_key = $right_key + 1, level = $level + 1[дополнительные параметры]
               var objNewNode = new Group() {
                  Name = textBox1.Text,
                  LeftKey = objParentNode.RightKey - 2,
                  RightKey = objParentNode.RightKey - 1,
                  Level = objParentNode.Level + 1
               };
               Form1.objModel.Groups.Add(objNewNode);
               Form1.objModel.SaveChanges();
            }
            this.Close();
         }
         catch(Exception ex) {
            MessageBox.Show(ex.Message);
         }
      }
Beispiel #2
0
      /// <summary>
      /// this method create TreeView from NestedSetsTree
      /// </summary>
      /// <param name="objLogicNode">node of NestedSetsTree</param>
      /// <param name="objVisualNode">node of TreeView</param>
      private void CreateTreeView(Group objLogicNode, TreeNode objVisualNode) {
         //выборка всех recievers котоыре принадлежат к текущей группе
         var objReceiversList = objModel.Receivers.Where(r => r.Group.Id == objLogicNode.Id).ToList();

         foreach(var reciever in objReceiversList) {
            var objNewTreeNode = new TreeNode() {
               Tag = reciever.Id,
               Name = reciever.Name,
               Text = reciever.Name
            };
            objVisualNode.Nodes.Add(objNewTreeNode);
         }
         //выбор подчиненных узлов на первом уровне, т.е на всех непосредсвенных детей родительского узла
         //SELECT id, name, level FROM my_tree WHERE left_key >= $left_key AND right_key <= $right_key ORDER BY left_key
         var objChildNodesList = objModel.Groups.
            Where(gr => gr.LeftKey > objLogicNode.LeftKey && gr.RightKey < objLogicNode.RightKey && gr.Level == objLogicNode.Level + 1).
            OrderBy(b => b.LeftKey).
            ToList();

         //add all childs on one level
         foreach(var node in objChildNodesList) {
            var objNewTreeNode = new TreeNode() {
               Tag = node.Id,
               Name = node.Name,
               Text = node.Name
            };
            objVisualNode.Nodes.Add(objNewTreeNode);
            CreateTreeView(node, objNewTreeNode);
         }
      }