private void button1_Click(object sender, EventArgs e) { string name = textBox2.Text; if (string.IsNullOrEmpty(name)) return; using (DataContainer c = new DataContainer()) { int id = Convert.ToInt32(Tree1.SelectedNode.Tag); Category parent = c.Category.First(p => p.Id == id); Category category = new Category(); category.Name = name; category.Parent = parent; c.AddToCategory(category); c.SaveChanges(); } //LoadInfo(); }
private void LoadInfo() { using (DataContainer c = new DataContainer()) { var allCategorys = c.Category.ToList(); if (allCategorys.Count == 0 || allCategorys.Any(p => p.Name == "root") == false) { Category root_add = new Category(); root_add.Name = "root"; root_add.Parent = null; c.AddToCategory(root_add); c.SaveChanges(); } Category root = c.Category.Where(p => p.Name == "root").First(); Tree1.Nodes.Clear(); var nodes = GetTreeNodes(root); nodes.ExpandAll(); Tree1.Nodes.Add(nodes); } }