Example #1
0
        private void miUpdate_Click(object sender, RoutedEventArgs e)
        {
            if (lstViewCar.SelectedItems.Count != 1)
            {
                MessageBox.Show("Please choose one item to update", "Information");
                return;
            }

            try
            {
                int           index         = lstViewCar.SelectedIndex;
                AddEditDialog addEditDialog = new AddEditDialog((Car)lstViewCar.SelectedItem);
                addEditDialog.Owner = this;

                addEditDialog.AddNewCarCallback += (c) => { currentCar = c; };
                bool?result = addEditDialog.ShowDialog();   // this line must be stay after the assignment, otherwise value is not assigned

                if (result == true)
                {
                    db.UpdateCar(currentCar);
                    carList.ElementAt(index).Id         = currentCar.Id;
                    carList.ElementAt(index).MakeModel  = currentCar.MakeModel;
                    carList.ElementAt(index).EngineSize = currentCar.EngineSize;
                    carList.ElementAt(index).FuelType   = currentCar.FuelType;
                }
            }
            catch (DataInvalidException ex)
            {
                MessageBox.Show(ex.Message, "Error Information");
            }
        }
Example #2
0
        private void CommandBinding_AddNewCar(object sender, ExecutedRoutedEventArgs e)
        {
            try
            {
                AddEditDialog addEditDialog = new AddEditDialog(null);
                addEditDialog.Owner = this;

                addEditDialog.AddNewCarCallback += (c) => { currentCar = c; };
                bool?result = addEditDialog.ShowDialog();   // this line must be stay after the assignment, otherwise value is not assigned

                if (result == true)
                {
                    int newId = db.AddCar(currentCar);
                    currentCar.Id = newId;
                    carList.Add(currentCar);
                }
            }
            catch (DataInvalidException ex)
            {
                MessageBox.Show(ex.Message, "Error Information");
            }
            lstViewCar.SelectedIndex = lstViewCar.Items.Count - 1;//for status bar selection change
        }