private void btnNewRootCategory_Click(object sender, EventArgs e) { using (Entity.StoryDBEntities context = new Entity.StoryDBEntities(ConnectionString)) { TreeNode newRoot = treeViewCategory.Nodes.Add(""); newRoot.Tag = -1; newRoot.BeginEdit(); } }
public void LoadCategoryTree() { using (Entity.StoryDBEntities context = new Entity.StoryDBEntities(ConnectionString)) { treeViewCategory.Nodes.Clear(); var rootQ = from c in context.Categories where !c.ParentCategoryId.HasValue || c.ParentCategoryId.Value == 0 select c; foreach (var n in rootQ) { TreeNode node = treeViewCategory.Nodes.Add(n.CategoryName); node.Tag = n.CategoryId; LoadSubCategorys(context, node, n.CategoryId); } } }
private void treeViewCategory_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) { using (Entity.StoryDBEntities context = new Entity.StoryDBEntities(ConnectionString)) { e.CancelEdit = true; try { if (!string.IsNullOrWhiteSpace(e.Label)) { int catId = (int)e.Node.Tag; int parentCatId = 0; if (e.Node.Parent != null) { parentCatId = (int)e.Node.Parent.Tag; } if (catId <= 0) { Entity.Category cat = new Entity.Category() { ParentCategoryId = parentCatId, CategoryName = e.Label, CreateBy = UpdateSourceName, CreateDate = DateTime.UtcNow, UpdateBy = UpdateSourceName, UpdateDate = DateTime.UtcNow, }; context.Categories.AddObject(cat); try { context.SaveChanges(); e.CancelEdit = false; } catch (Exception x) { MessageBox.Show(x.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { Entity.Category cat = ( from c in context.Categories where c.CategoryId == catId select c).SingleOrDefault(); cat.CategoryName = e.Label; cat.UpdateBy = UpdateSourceName; cat.UpdateDate = DateTime.UtcNow; context.SaveChanges(); e.CancelEdit = false; } } } finally { if (e.CancelEdit) e.Node.BeginEdit(); } } }