/// <summary> /// insert new department /// </summary> private void Insert() { try { var department = new cat_Department { Name = txtName.Text.Trim(), ShortName = txtShortName.Text.Trim(), Address = txtAddress.Text.Trim(), Telephone = txtTelephone.Text.Trim(), Fax = txtFax.Text.Trim(), TaxCode = txtTaxCode.Text.Trim(), Order = 0, ParentId = 0, IsPrimary = chkIsPrimary.Checked, IsLocked = chkIsLocked.Checked }; if (int.TryParse(cbxParent.SelectedItem.Value, out var parentId) && parentId >= 0) { department.ParentId = parentId; } if (int.TryParse(txtOrder.Text, out var order)) { department.Order = order; } cat_DepartmentServices.Create(department); } catch (Exception e) { Dialog.Alert("Có lỗi xảy ra trong quá trình thêm mới: {0}".FormatWith(e.Message)); } }
/// <summary> /// Recursive function for populating child node /// </summary> /// <param name="lstDepartments"></param> /// <param name="currentDepartment"></param> /// <param name="level"></param> /// <param name="departmentTree"></param> private void PopulateDepartmentsTreeNode(List <cat_Department> lstDepartments, cat_Department currentDepartment, int level, ref List <cat_Department> departmentTree) { var lstChildDepartments = lstDepartments.Where(d => d.ParentId == currentDepartment.Id).OrderBy(d => d.Order); level += 1; var prefix = string.Empty; for (var i = 0; i < level; i++) { prefix += @"+---"; } foreach (var d in lstChildDepartments) { d.Name = "{0}{1}".FormatWith(prefix, d.Name); departmentTree.Add(d); PopulateDepartmentsTreeNode(lstDepartments, d, level, ref departmentTree); } }