Exemplo n.º 1
0
        /// <summary>
        /// Handles the UserDeletingRow event of the _customersGrid control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewRowCancelEventArgs"/> instance containing the event data.</param>
        private void _customersGrid_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            DialogResult res = MessageBox.Show("Are you sure you want to delete this customer?", "Delete customer", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            // the user wants to delete the current customer
            if (res == DialogResult.Yes)
            {
                // delete the current customer
                try
                {
                    CustomerEntity customerToDelete = (CustomerEntity)e.Row.DataBoundItem;
                    customerToDelete.Delete();
                }

                // there are entity error on the delete action
                catch (ORMEntityValidationException ex)
                {
                    e.Cancel = true;
                    MessageBox.Show(ex.Message, "Errors", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            // cancel the delete row operation
            else
            {
                e.Cancel = true;
            }
        }