private void addButton_Click(object sender, RoutedEventArgs e) { ToDoItem item = new ToDoItem(); AddDialog addDialog = new AddDialog(ref item); Nullable<bool> dialogResult = addDialog.ShowDialog(); if (dialogResult == true) { //Get new Index if (ToDoList.Count == 0) item.Index = 0; else { var maxIndex = ToDoList.Max(m => m.Index); item.Index = ++maxIndex; } //Get new GUID item.Id = Guid.NewGuid(); //Save the item to db int rowsAffected = 0; using (ToDoEntities da = new ToDoEntities()) { da.ToDoItems.AddObject(item); rowsAffected = da.SaveChanges(); } // Save the item to the list if (rowsAffected != 0) ToDoList.Add(item); else MessageBox.Show("Error while accessing the database", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void editButton_Click(object sender, RoutedEventArgs e) { // Retrieve the item being edited ToDoItem item = ((Button)sender).DataContext as ToDoItem; // Call the Add/Edit window AddDialog addDialog = new AddDialog(ref item); Nullable<bool> dialogResult = addDialog.ShowDialog(); // Save the edited item if (dialogResult == true) { //Save the changes to the database int rowsAffected = 0; using (ToDoEntities da = new ToDoEntities()) { ToDoItem daItem = da.ToDoItems.FirstOrDefault<ToDoItem>(p => p.Id == item.Id); daItem.Index = item.Index; daItem.Title = item.Title; daItem.Description = item.Description; daItem.DueDateTime = item.DueDateTime; daItem.Done = item.Done; rowsAffected = da.SaveChanges(); } if (rowsAffected != 0) { int index = ToDoList.IndexOf(item); ToDoList[index] = item; } else MessageBox.Show("Error while accessing the database", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }