Пример #1
0
        private void butonSave_Click(object sender, RoutedEventArgs e)
        {
            Order order = null;

            if (action == ActionState.New)
            {
                try
                {
                    Customer customer = (Customer)cmbCustomers.SelectedItem;
                    Catalog  catalog  = (Catalog)cmbCatalog.SelectedItem;
                    //instantiem Order entity
                    order = new Order()
                    {
                        CustId       = customer.CustId,
                        MotorcycleId = catalog.MotorcycleId
                    };
                    //adaugam entitatea nou creata in context
                    ctx.Orders.Add(order);
                    catalogOrdersViewSource.View.Refresh();
                    //salvam modificarile
                    ctx.SaveChanges();
                    SetValidationBinding();
                }
                catch (DataException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                customerViewSource.View.Refresh();
            }
            if (action == ActionState.Edit)
            {
                dynamic selectedOrder = ordersDataGrid.SelectedItem;
                try
                {
                    int curr_id     = selectedOrder.OrderId;
                    var editedOrder = ctx.Orders.FirstOrDefault(s => s.OrderId == curr_id);
                    if (editedOrder != null)
                    {
                        editedOrder.CustId       = Int32.Parse(cmbCustomers.SelectedValue.ToString());
                        editedOrder.MotorcycleId = Convert.ToInt32(cmbCatalog.SelectedValue.ToString());
                        //salvam modificarile
                        ctx.SaveChanges();
                        SetValidationBinding();
                    }
                }
                catch (DataException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                BindDataGrid();
                // pozitionarea pe item-ul curent
                customerViewSource.View.MoveCurrentTo(selectedOrder);
            }
            else if (action == ActionState.Delete)
            {
                try
                {
                    dynamic selectedOrder = ordersDataGrid.SelectedItem;
                    int     curr_id       = selectedOrder.OrderId;
                    var     deletedOrder  = ctx.Orders.FirstOrDefault(s => s.OrderId == curr_id);
                    if (deletedOrder != null)
                    {
                        ctx.Orders.Remove(deletedOrder);
                        ctx.SaveChanges();
                        MessageBox.Show("Order Deleted Successfully", "Message");
                        BindDataGrid();
                    }
                }
                catch (DataException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Пример #2
0
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Customer customer = null;

            if (action == ActionState.New)
            {
                try
                {
                    customer = new Customer()
                    {
                        First_Name = firstNameTextBox.Text.Trim(),
                        Last_Name  = lastNameTextBox.Text.Trim(),
                        Country    = countryTextBox.Text.Trim()
                    };
                    //adaugam entitatea nou creata in context
                    ctx.Customers.Add(customer);
                    customerViewSource.View.Refresh();
                    //salvam modificarile
                    ctx.SaveChanges();
                    SetValidationBinding();
                }
                catch (DataException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                customerViewSource.View.Refresh();
            }
            else
            if (action == ActionState.Edit)
            {
                try
                {
                    customer            = (Customer)customerDataGrid.SelectedItem;
                    customer.First_Name = firstNameTextBox.Text.Trim();
                    customer.Last_Name  = lastNameTextBox.Text.Trim();
                    customer.Country    = countryTextBox.Text.Trim();
                    //salvam modificarile
                    ctx.SaveChanges();
                    SetValidationBinding();
                }
                catch (DataException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                customerViewSource.View.Refresh();
                // pozitionarea pe item-ul curent
                customerViewSource.View.MoveCurrentTo(customer);
            }
            else
            if (action == ActionState.Delete)
            {
                try
                {
                    customer = (Customer)customerDataGrid.SelectedItem;
                    ctx.Customers.Remove(customer);
                    ctx.SaveChanges();
                }
                catch (DataException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                customerViewSource.View.Refresh();
            }
        }