Exemple #1
0
        private void CurrentCustomerDetails_Click(object sender, EventArgs e)
        {
            CustomerEntity customerEntity;

            try
            {
                customerEntity = _filtered ?
                                 _customerViewFilter.CurrentCustomer(_customerBindingSource.Position) :
                                 _customerView.CurrentCustomer(_customerBindingSource.Position);

                var displayCustomerForm = new CustomerEntityReadOnlyForm(customerEntity);

                try
                {
                    displayCustomerForm.ShowDialog();
                }
                finally
                {
                    displayCustomerForm.Dispose();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemple #2
0
        /// <summary>
        /// Performs simple validation and setting property values so on any changes
        /// they are updated immediately.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _customerView_ListChanged(object sender, ListChangedEventArgs e)
        {
            if (e.PropertyDescriptor != null)
            {
                //
                // Get current customer in current DataGridView row
                //
                CustomerEntity currentCustomer = _customerView.CurrentCustomer(_customerBindingSource.Position);
                Console.WriteLine($" -> {e.ListChangedType.ToString()}");
                //
                // Assert if there are changes to the current row in the DataGridView
                //
                if (e.ListChangedType == ListChangedType.ItemChanged)
                {
                    //
                    // Get data residing in the database table
                    //
                    Customers customerItem = CustomersTestOperations
                                             .Context
                                             .Customers
                                             .FirstOrDefault((customer) => customer.CustomerIdentifier == currentCustomer.CustomerIdentifier);

                    if (customerItem != null)
                    {
                        /*
                         * Did contact first or last name change?
                         * If so we need to update the contact.
                         */
                        if (e.PropertyDescriptor.DisplayName == "FirstName" || e.PropertyDescriptor.DisplayName == "LastName")
                        {
                            customerItem.Contact.FirstName = currentCustomer.FirstName;
                            customerItem.Contact.LastName  = currentCustomer.LastName;
                        }

                        var test1 = currentCustomer.CompanyName;

                        EntityEntry <Customers> customerEntry = CustomersTestOperations.Context.Entry(customerItem);
                        customerEntry.CurrentValues.SetValues(currentCustomer);

                        //
                        // Setup validation on current row data
                        //
                        var validation = ValidationHelper.ValidateEntity(currentCustomer);

                        //
                        // If there are validation error present them to the user
                        //
                        if (validation.HasError)
                        {
                            var errorItems = string.Join(Environment.NewLine,
                                                         validation.Errors.Select((containerItem) => containerItem.ErrorMessage).ToArray());

                            MessageBox.Show(errorItems + Environment.NewLine + @"Customer has been reset!!!", @"Corrections needed");

                            //
                            // Read current values from database
                            //
                            Customers originalCustomer = CustomersTestOperations.CustomerFirstOrDefault(customerItem.CustomerIdentifier);

                            //
                            // reset current item both in Customer object and CustomerEntity object
                            // (there are other ways to deal with this but are dependent on business logic)
                            //
                            customerEntry.CurrentValues.SetValues(originalCustomer);
                            _customerView[_customerBindingSource.Position] = CustomersTestOperations.CustomerByIdentifier(originalCustomer.CustomerIdentifier);
                            _hasValidationErrors = true;
                        }
                        else
                        {
                            _hasValidationErrors = false;
                            CustomersTestOperations.Context.SaveChanges();
                        }
                    }
                }
            }
        }