Пример #1
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            var dbContext = AppVariables.getDbContext();
            // создание нового экземпляра сущности
            var customer = (CUSTOMER)bindingSource.AddNew();

            // создаём форму для редактирования
            using (CustomerEditorForm editor = new CustomerEditorForm())
            {
                editor.Text     = "Add customer";
                editor.Customer = customer;
                // Обработчик закрытия формы
                editor.FormClosing += delegate(object fSender, FormClosingEventArgs fe)
                {
                    if (editor.DialogResult == DialogResult.OK)
                    {
                        try
                        {
                            // получаем новое значение генератора
                            // и присваиваем его идентификатору
                            customer.CUSTOMER_ID = dbContext.NextValueFor("GEN_CUSTOMER_ID");
                            // добавляем нового заказчика
                            dbContext.CUSTOMERS.Add(customer);
                            // пытаемся сохранить изменения
                            dbContext.SaveChanges();
                            // и обновить текущую запись
                            dbContext.Refresh(RefreshMode.StoreWins, customer);
                        }
                        catch (Exception ex)
                        {
                            // отображаем ошибку
                            MessageBox.Show(ex.Message, "Error");
                            // не закрываем форму для возможности исправления ошибки
                            fe.Cancel = true;
                        }
                    }
                    else
                    {
                        bindingSource.CancelEdit();
                    }
                };
                // показываем модальную форму
                editor.ShowDialog(this);
            }
        }
Пример #2
0
        private void btnEdit_Click(object sender, EventArgs e)
        {
            var dbContext = AppVariables.getDbContext();
            // получаем сущность
            var customer = (CUSTOMER)bindingSource.Current;

            // создаём форму для редактирования
            using (CustomerEditorForm editor = new CustomerEditorForm())
            {
                editor.Text     = "Edit customer";
                editor.Customer = customer;
                // Обработчик закрытия формы
                editor.FormClosing += delegate(object fSender, FormClosingEventArgs fe)
                {
                    if (editor.DialogResult == DialogResult.OK)
                    {
                        try
                        {
                            // пытаемся сохранить изменения
                            dbContext.SaveChanges();
                            dbContext.Refresh(RefreshMode.StoreWins, customer);
                            // обновляем все связанные контролы
                            bindingSource.ResetCurrentItem();
                        }
                        catch (Exception ex)
                        {
                            // отображаем ошибку
                            MessageBox.Show(ex.Message, "Error");
                            // не закрываем форму для возможности исправления ошибки
                            fe.Cancel = true;
                        }
                    }
                    else
                    {
                        bindingSource.CancelEdit();
                    }
                };
                // показываем модальную форму
                editor.ShowDialog(this);
            }
        }