private void edit_Click(object sender, RoutedEventArgs e) { DataGridRow dgRow = (DataGridRow)(gridEmployee.ItemContainerGenerator.ContainerFromItem(gridEmployee.SelectedItem)); Employee selectedEmpo = gridEmployee.Items[gridEmployee.SelectedIndex] as Employee; if (dgRow == null) { return; } DataGridDetailsPresenter dgdPresenter = FindVisualChild <DataGridDetailsPresenter>(dgRow); DataTemplate template = dgdPresenter.ContentTemplate; TextBox textBoxFName = (TextBox)template.FindName("editFirstName", dgdPresenter); TextBox textBoxLName = (TextBox)template.FindName("editLastName", dgdPresenter); TextBox textBoxDepID = (TextBox)template.FindName("editDepID", dgdPresenter); TextBox textBoxSalary = (TextBox)template.FindName("editSalary", dgdPresenter); TextBox textBoxSuper = (TextBox)template.FindName("editSuper", dgdPresenter); if (whoUse.Hierarchy == Hierarchy.Clubmember || whoUse.Hierarchy == Hierarchy.Customer || whoUse.Hierarchy == Hierarchy.Worker) { textBoxFName.IsReadOnly = true; textBoxLName.IsReadOnly = true; textBoxDepID.IsReadOnly = true; textBoxSalary.IsReadOnly = true; textBoxSuper.IsReadOnly = true; MessageBox.Show("You are not allowed to edit this field"); } else { if (textBoxFName == null || textBoxLName == null || textBoxDepID == null || textBoxSalary == null || textBoxSuper == null) { MessageBox.Show("you have to fill all the empty fields"); } else { try { if (textBoxFName.Text.Length < 1) { throw new Exception("Invalid First Name, Must Have Atleast One Character"); } selectedEmpo.FirstName = textBoxFName.Text; if (textBoxFName.Text.Length < 1) { throw new Exception("Invalid Last Name, Must Have Atleast One Character"); } selectedEmpo.LastName = textBoxLName.Text; InputCheck.isInt(textBoxDepID.Text, " department ID "); selectedEmpo.DepartmentID = Convert.ToInt32(textBoxDepID.Text); InputCheck.isDouble(textBoxSalary.Text, " salary "); selectedEmpo.Salary = Convert.ToDouble(textBoxSalary.Text); InputCheck.isInt(textBoxSuper.Text, " supervisor ID "); selectedEmpo.SupervisorID = Convert.ToInt32(textBoxSuper.Text); itsBL.edit(selectedEmpo); itsBL.saveDataToFile(); MessageBox.Show("Changes have been done"); tablePanel.Children.Clear(); DepartmentT showProduct = new DepartmentT(itsBL, Permanent, whoUse); tablePanel.Children.Add(showProduct); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }
private void edit_Click(object sender, RoutedEventArgs e) { DataGridRow dgRow = (DataGridRow)(gridProduct.ItemContainerGenerator.ContainerFromItem(gridProduct.SelectedItem)); Product selectedProd = gridProduct.Items[gridProduct.SelectedIndex] as Product; if (dgRow == null) { return; } DataGridDetailsPresenter dgdPresenter = FindVisualChild <DataGridDetailsPresenter>(dgRow); DataTemplate template = dgdPresenter.ContentTemplate; TextBox textBoxName = (TextBox)template.FindName("editName", dgdPresenter); TextBox textBoxLoc = (TextBox)template.FindName("editLocation", dgdPresenter); TextBox textBoxStock = (TextBox)template.FindName("editStock", dgdPresenter); TextBox textBoxPrice = (TextBox)template.FindName("editPrice", dgdPresenter); TextBox textBoxWhen = (TextBox)template.FindName("editWhen", dgdPresenter); ComboBox textBoxType = (ComboBox)template.FindName("Atype", dgdPresenter); editType = textBoxType.Text; if (whoUse.Hierarchy == Hierarchy.Clubmember || whoUse.Hierarchy == Hierarchy.Customer || whoUse.Hierarchy == Hierarchy.Worker) { textBoxName.IsReadOnly = true; textBoxLoc.IsReadOnly = true; textBoxStock.IsReadOnly = true; textBoxPrice.IsReadOnly = true; textBoxWhen.IsReadOnly = true; textBoxType.IsReadOnly = true; MessageBox.Show("You are not allowed to edit this field"); } else { if (editType == "Home And Garden") { editType = "HomeAndGarden"; } if (textBoxName == null || textBoxLoc == null || textBoxStock == null || textBoxPrice == null || textBoxWhen == null || textBoxType == null) { MessageBox.Show("you have to fill all the empty fields"); } else { try { selectedProd.Name = textBoxName.Text; if (textBoxName.Text.Length < 1) { throw new Exception("Invalid First Name, Must Have Atleast One Character"); } InputCheck.isType(editType); selectedProd.Type = (Backend.Type)Enum.Parse(typeof(Backend.Type), editType); InputCheck.isInt(textBoxLoc.Text, " location (Department ID) "); selectedProd.Location = Convert.ToInt32(textBoxLoc.Text); InputCheck.isInt(textBoxStock.Text, " units in stock "); selectedProd.StockCount = Convert.ToInt32(textBoxStock.Text); InputCheck.isDouble(textBoxPrice.Text, " price "); selectedProd.Price = Convert.ToDouble(textBoxPrice.Text); InputCheck.isInt(textBoxWhen.Text, " 'when to order' "); selectedProd.WhenToOrder = Convert.ToInt32(textBoxWhen.Text); itsBL.edit(selectedProd); itsBL.saveDataToFile(); MessageBox.Show("Changes have been done"); tablePanel.Children.Clear(); DepartmentT showProduct = new DepartmentT(itsBL, permanent, whoUse); tablePanel.Children.Add(showProduct); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }