private void AddSubtreeForItem(HierarchyRow row, WC.TreeNode item) { HierarchyRow[] children = row.GetHierarchyChildRows("Hierarchy"); WC.TreeNode childItem; foreach (HierarchyRow child in children) { childItem = new WC.TreeNode((child.Caption ?? "NULL"), child["NavId"].ToString()); item.ChildNodes.Add(childItem); AddSubtreeForItem(child, childItem); } }
/// <summary> /// renames the selected TreeNode (if any) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void OnRenameButtonClicked(object sender, EventArgs e) { if (newLabelTB.Text == "" || menuTree.SelectedNode == null) { return; } menuTree.SelectedNode.Text = newLabelTB.Text; HierarchyRow toRename = menuHierarchy.Find(Int32.Parse(menuTree.SelectedValue)); toRename.Caption = newLabelTB.Text; RecreateChildControls(); }
/// <summary> /// adds a new tree node as a next parent of the currently selected one /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void OnAddButtonClicked(object sender, EventArgs e) { if (newLabelTB.Text == "") { return; } HierarchyRow newRow = (HierarchyRow)(menuHierarchy.NewRow()); newRow.Caption = newLabelTB.Text; newRow.NavId = null; newRow.ParentId = null; if (menuTree.SelectedNode != null) { newRow.ParentId = Int32.Parse(menuTree.SelectedValue); } menuHierarchy.Rows.Add(newRow); RecreateChildControls(); }
/// <summary> /// removes the selected TreeNode (if any); if it has any children, they leave too! /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void OnRemoveButtonClicked(object sender, EventArgs e) { if (menuTree.SelectedNode == null) { return; } HierarchyRow toRemove = menuHierarchy.Find(Int32.Parse(menuTree.SelectedValue)); hierarchyDataset.Relations["Hierarchy"].ChildKeyConstraint.DeleteRule = Rule.Cascade; menuHierarchy.Rows.Remove(toRemove); // the cascading constraint deletes all the child rows of the deleted row, but that only sets them to "Deleted", // they remain in the table (http://msmvps.com/blogs/williamryan/archive/2005/05/10/46445.aspx). List <HierarchyRow> deletedRows = (from HierarchyRow r in menuHierarchy.Rows where r.RowState == DataRowState.Deleted select r) .ToList <HierarchyRow>(); foreach (HierarchyRow r in deletedRows) { menuHierarchy.Rows.Remove(r); } RecreateChildControls(); }