/// <summary> /// The main method of this class. It deletes one or more rows selected in a grid on the caller form /// </summary> /// <param name="ACallerFormOrControl">The form or user control that is making the call. The form must implement the IDeleteGridRows interface</param> /// <param name="AGrid">A reference to the grid object</param> /// <param name="APetraUtilsObject">A reference to the PetraUtilsObject associated with the form or control making the call</param> /// <param name="AButtonPanel">A reference a form or control that implements the IButtonPanel interface. This parameter can be null.</param> /// <returns>True if any rows were actually deleted</returns> public static bool DeleteRows(IDeleteGridRows ACallerFormOrControl, TSgrdDataGrid AGrid, TFrmPetraEditUtils APetraUtilsObject, IButtonPanel AButtonPanel) { DataRow currentDataRow = ACallerFormOrControl.GetSelectedDataRow(); Int32 currentRowIndex = ACallerFormOrControl.GetSelectedRowIndex(); if ((currentDataRow == null) || (currentRowIndex == -1)) { return(false); } string CompletionMessage = String.Empty; DataRowView[] HighlightedRows = AGrid.SelectedDataRowsAsDataRowView; if (HighlightedRows.Length == 1) { // Single row deletion TVerificationResultCollection VerificationResults = null; if (TVerificationHelper.IsNullOrOnlyNonCritical(APetraUtilsObject.VerificationResultCollection)) { ACallerFormOrControl.GetReferenceCount(currentDataRow, APetraUtilsObject.MaxReferenceCountOnDelete, out VerificationResults); } if ((VerificationResults != null) && (VerificationResults.Count > 0)) { TCascadingReferenceCountHandler countHandler = new TCascadingReferenceCountHandler(); TFrmExtendedMessageBox.TResult result = countHandler.HandleReferences(APetraUtilsObject, VerificationResults, true); if (result == TFrmExtendedMessageBox.TResult.embrYes) { // repeat the count but with no limit to the number of references ACallerFormOrControl.GetReferenceCount(currentDataRow, 0, out VerificationResults); countHandler.HandleReferences(APetraUtilsObject, VerificationResults, false); } return(false); } string DeletionQuestion = ACallerFormOrControl.GetDefaultDeletionQuestion(); bool AllowDeletion = true; bool DeletionPerformed = false; ACallerFormOrControl.HandlePreDelete(currentDataRow, ref AllowDeletion, ref DeletionQuestion); if (AllowDeletion) { if ((MessageBox.Show(DeletionQuestion, MCommonResourcestrings.StrConfirmDeleteTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)) { try { if (!ACallerFormOrControl.HandleDeleteRow(currentDataRow, ref DeletionPerformed, ref CompletionMessage)) { currentDataRow.Delete(); DeletionPerformed = true; } } catch (Exception ex) { MessageBox.Show(String.Format(MCommonResourcestrings.StrErrorWhileDeleting, Environment.NewLine, ex.Message), MCommonResourcestrings.StrGenericError, MessageBoxButtons.OK, MessageBoxIcon.Warning); } if (DeletionPerformed) { APetraUtilsObject.SetChangedFlag(); } // Select and display the details of the nearest row to the one previously selected ACallerFormOrControl.SelectRowInGrid(currentRowIndex); // Clear any errors left over from the deleted row APetraUtilsObject.VerificationResultCollection.Clear(); if (AButtonPanel != null) { AButtonPanel.UpdateRecordNumberDisplay(); } } } if (!ACallerFormOrControl.HandlePostDelete(currentDataRow, AllowDeletion, DeletionPerformed, CompletionMessage)) { if (DeletionPerformed && (CompletionMessage.Length > 0)) { MessageBox.Show(CompletionMessage, MCommonResourcestrings.StrDeletionCompletedTitle); } } return(DeletionPerformed); } else { // Multi-row deletion int recordsDeleted = 0; string DeletionQuestion = String.Format(MCommonResourcestrings.StrMultiRowDeletionQuestion, HighlightedRows.Length, Environment.NewLine); DeletionQuestion += MCommonResourcestrings.StrMultiRowDeletionCheck; if (MessageBox.Show(DeletionQuestion, MCommonResourcestrings.StrConfirmDeleteTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes) { int recordsUndeletable = 0; int recordsDeleteDisallowed = 0; List <TMultiDeleteResult> listConflicts = new List <TMultiDeleteResult>(); List <TMultiDeleteResult> listExceptions = new List <TMultiDeleteResult>(); APetraUtilsObject.GetForm().Cursor = Cursors.WaitCursor; foreach (DataRowView drv in HighlightedRows) { DataRow rowToDelete = drv.Row; if (rowToDelete.RowState == DataRowState.Detached) { continue; } string rowDetails = MakePKValuesString(rowToDelete); if (!ACallerFormOrControl.IsRowDeletable(rowToDelete)) { recordsUndeletable++; continue; } TVerificationResultCollection VerificationResults = null; ACallerFormOrControl.GetReferenceCount(rowToDelete, APetraUtilsObject.MaxReferenceCountOnDelete, out VerificationResults); if ((VerificationResults != null) && (VerificationResults.Count > 0)) { TMultiDeleteResult result = new TMultiDeleteResult(rowDetails, Messages.BuildMessageFromVerificationResult(String.Empty, VerificationResults)); listConflicts.Add(result); continue; } bool AllowDeletion = true; bool DeletionPerformed = false; ACallerFormOrControl.HandlePreDelete(rowToDelete, ref AllowDeletion, ref DeletionQuestion); if (AllowDeletion) { try { if (!ACallerFormOrControl.HandleDeleteRow(rowToDelete, ref DeletionPerformed, ref CompletionMessage)) { rowToDelete.Delete(); DeletionPerformed = true; } } catch (Exception ex) { TMultiDeleteResult result = new TMultiDeleteResult(rowDetails, ex.Message); listExceptions.Add(result); } } else { recordsDeleteDisallowed++; } if (DeletionPerformed) { APetraUtilsObject.SetChangedFlag(); recordsDeleted++; } ACallerFormOrControl.HandlePostDelete(rowToDelete, AllowDeletion, DeletionPerformed, String.Empty); } APetraUtilsObject.GetForm().Cursor = Cursors.Default; ACallerFormOrControl.SelectRowInGrid(currentRowIndex); if (AButtonPanel != null) { AButtonPanel.UpdateRecordNumberDisplay(); } if ((recordsDeleted > 0) && (CompletionMessage.Length > 0)) { MessageBox.Show(CompletionMessage, MCommonResourcestrings.StrDeletionCompletedTitle); } // Show the results of the multi-deletion string results = null; if (recordsDeleted > 0) { results = String.Format( Catalog.GetPluralString(MCommonResourcestrings.StrRecordSuccessfullyDeleted, MCommonResourcestrings.StrRecordsSuccessfullyDeleted, recordsDeleted), recordsDeleted); } else { results = MCommonResourcestrings.StrNoRecordsWereDeleted; } if (recordsUndeletable > 0) { results += String.Format( Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedBecauseNonDeletable, MCommonResourcestrings.StrRowsNotDeletedBecauseNonDeletable, recordsUndeletable), Environment.NewLine, recordsUndeletable); } if (recordsDeleteDisallowed > 0) { results += String.Format( Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedBecauseDeleteNotAllowed, MCommonResourcestrings.StrRowsNotDeletedBecauseDeleteNotAllowed, recordsDeleteDisallowed), Environment.NewLine, recordsDeleteDisallowed); } bool showCancel = false; if (listConflicts.Count > 0) { showCancel = true; results += String.Format( Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedBecauseReferencedElsewhere, MCommonResourcestrings.StrRowsNotDeletedBecauseReferencedElsewhere, listConflicts.Count), Environment.NewLine, listConflicts.Count); } if (listExceptions.Count > 0) { showCancel = true; results += String.Format( Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedDueToUnexpectedException, MCommonResourcestrings.StrRowNotDeletedDueToUnexpectedException, listExceptions.Count), Environment.NewLine, listExceptions.Count); } if (showCancel) { results += String.Format(MCommonResourcestrings.StrClickToReviewDeletionOrCancel, Environment.NewLine); if (MessageBox.Show(results, MCommonResourcestrings.StrDeleteActionSummaryTitle, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.OK) { ReviewMultiDeleteResults(listConflicts, MCommonResourcestrings.StrRowsReferencedByOtherTables); ReviewMultiDeleteResults(listExceptions, MCommonResourcestrings.StrExceptions); } } else { MessageBox.Show(results, MCommonResourcestrings.StrDeleteActionSummaryTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); } } return(recordsDeleted > 0); } }
/// <summary> /// Standard method to delete the Data Row whose Details are currently displayed. /// There is full support for multi-row deletion. /// Optional manual code can be included to take action prior, during or after each deletion. /// When the row(s) have been deleted the highlighted row index stays the same unless the deleted row was the last one. /// The Details for the newly highlighted row are automatically displayed - or not, if the grid has now become empty. /// </summary> private void DeletePPostcodeRegionRange() { string CompletionMessage = String.Empty; if ((FPreviouslySelectedRangeRow == null) || (FPrevRangeRowChangedRow == -1)) { return; } DataRowView[] HighlightedRows = grdRanges.SelectedDataRowsAsDataRowView; if (HighlightedRows.Length == 1) { TVerificationResultCollection VerificationResults = null; if (TVerificationHelper.IsNullOrOnlyNonCritical(FPetraUtilsObject.VerificationResultCollection)) { this.Cursor = Cursors.WaitCursor; TRemote.MPartner.ReferenceCount.WebConnectors.GetNonCacheableRecordReferenceCount( FMainDS.PPostcodeRegionRange, DataUtilities.GetPKValuesFromDataRow(FPreviouslySelectedRangeRow), FPetraUtilsObject.MaxReferenceCountOnDelete, out VerificationResults); this.Cursor = Cursors.Default; } if ((VerificationResults != null) && (VerificationResults.Count > 0)) { MessageBox.Show(Messages.BuildMessageFromVerificationResult( Catalog.GetString("Record cannot be deleted!") + Environment.NewLine + Catalog.GetPluralString("Reason:", "Reasons:", VerificationResults.Count), VerificationResults), Catalog.GetString("Record Deletion")); return; } string DeletionQuestion = Catalog.GetString("Are you sure you want to delete the current row?"); if ((FPrimaryKeyControl != null) && (FPrimaryKeyLabel != null)) { DeletionQuestion += String.Format("{0}{0}({1} {2})", Environment.NewLine, "Range Name:", FPreviouslySelectedRangeRow.Range); } bool AllowDeletion = true; bool DeletionPerformed = false; if (AllowDeletion) { if ((MessageBox.Show(DeletionQuestion, Catalog.GetString("Confirm Delete"), MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)) { try { FPreviouslySelectedRangeRow.Delete(); DeletionPerformed = true; } catch (Exception ex) { MessageBox.Show(String.Format(Catalog.GetString("An error occurred while deleting this record.{0}{0}{1}"), Environment.NewLine, ex.Message), Catalog.GetString("Error"), MessageBoxButtons.OK, MessageBoxIcon.Warning); } if (DeletionPerformed) { FPetraUtilsObject.SetChangedFlag(); } // Select and display the details of the nearest row to the one previously selected grdRanges.SelectRowInGrid(FPrevRangeRowChangedRow, true); // Clear any errors left over from the deleted row FPetraUtilsObject.VerificationResultCollection.Clear(); } } if (DeletionPerformed && (CompletionMessage.Length > 0)) { MessageBox.Show(CompletionMessage, Catalog.GetString("Deletion Completed")); } } else { string DeletionQuestion = String.Format(Catalog.GetString( "Do you want to delete the {0} highlighted rows?{1}{1}"), HighlightedRows.Length, Environment.NewLine); DeletionQuestion += Catalog.GetString("Each record will be checked to confirm that it can be deleted."); if (MessageBox.Show(DeletionQuestion, Catalog.GetString("Confirm Delete"), MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes) { int recordsDeleted = 0; int recordsUndeletable = 0; int recordsDeleteDisallowed = 0; List <TMultiDeleteResult>listConflicts = new List <TMultiDeleteResult>(); List <TMultiDeleteResult>listExceptions = new List <TMultiDeleteResult>(); this.Cursor = Cursors.WaitCursor; foreach (DataRowView drv in HighlightedRows) { PPostcodeRegionRangeRow rowToDelete = (PPostcodeRegionRangeRow)(drv.Row); string rowDetails = MakePKValuesStringManual(rowToDelete); TVerificationResultCollection VerificationResults = null; TRemote.MPartner.ReferenceCount.WebConnectors.GetNonCacheableRecordReferenceCount( FMainDS.PPostcodeRegionRange, DataUtilities.GetPKValuesFromDataRow(rowToDelete), FPetraUtilsObject.MaxReferenceCountOnDelete, out VerificationResults); if ((VerificationResults != null) && (VerificationResults.Count > 0)) { TMultiDeleteResult result = new TMultiDeleteResult(rowDetails, Messages.BuildMessageFromVerificationResult(String.Empty, VerificationResults)); listConflicts.Add(result); continue; } bool AllowDeletion = true; bool DeletionPerformed = false; if (AllowDeletion) { try { rowToDelete.Delete(); DeletionPerformed = true; } catch (Exception ex) { TMultiDeleteResult result = new TMultiDeleteResult(rowDetails, ex.Message); listExceptions.Add(result); } } else { recordsDeleteDisallowed++; } if (DeletionPerformed) { FPetraUtilsObject.SetChangedFlag(); recordsDeleted++; } } this.Cursor = Cursors.Default; // Select and display the details of the nearest row to the one previously selected grdRanges.SelectRowInGrid(FPrevRangeRowChangedRow, true); if ((recordsDeleted > 0) && (CompletionMessage.Length > 0)) { MessageBox.Show(CompletionMessage, Catalog.GetString("Deletion Completed")); } // Show the results of the multi-deletion string results = null; if (recordsDeleted > 0) { string s1 = Catalog.GetPluralString("record", "records", recordsDeleted); string s2 = Catalog.GetPluralString("was", "were", recordsDeleted); results = String.Format(Catalog.GetString("{0} {1} {2} successfully deleted."), recordsDeleted, s1, s2); } else { results = "No records were deleted."; } if (recordsUndeletable > 0) { string s1 = Catalog.GetPluralString("record", "records", recordsUndeletable); string s2 = Catalog.GetPluralString("it is marked", "they are marked", recordsUndeletable); results += String.Format(Catalog.GetString("{0}{1} {2} could not be deleted because {3} as non-deletable."), Environment.NewLine, recordsUndeletable, s1, s2); } if (recordsDeleteDisallowed > 0) { string s1 = Catalog.GetPluralString("record was not be deleted", "records were not be deleted", recordsUndeletable); results += String.Format(Catalog.GetString("{0}{1} {2} because deletion was not allowed."), Environment.NewLine, recordsDeleteDisallowed, s1); } bool showCancel = false; if (listConflicts.Count > 0) { showCancel = true; string s1 = Catalog.GetPluralString("record", "records", listConflicts.Count); string s2 = Catalog.GetPluralString("it is referenced", "they are referenced", listConflicts.Count); results += String.Format(Catalog.GetString("{0}{1} {2} could not be deleted because {3} by at least one other table."), Environment.NewLine, listConflicts.Count, s1, s2); } if (listExceptions.Count > 0) { showCancel = true; string s1 = Catalog.GetPluralString("record", "records", listExceptions.Count); results += String.Format(Catalog.GetString("{0}{1} {2} could not be deleted because the delete action failed unexpectedly."), Environment.NewLine, listExceptions.Count, s1); } if (showCancel) { results += String.Format(Catalog.GetString("{0}{0}Click OK to review the details, or Cancel to return direct to the data screen"), Environment.NewLine); if (MessageBox.Show(results, Catalog.GetString("Delete Action Summary"), MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.OK) { ReviewMultiDeleteResults(listConflicts, Catalog.GetString("Rows in this table that are referenced by other tables")); ReviewMultiDeleteResults(listExceptions, Catalog.GetString("Unexpected Exceptions")); } } else { MessageBox.Show(results, Catalog.GetString("Delete Action Summary"), MessageBoxButtons.OK, MessageBoxIcon.Information); } } } }
/// <summary> /// Standard method to delete the Data Row whose Details are currently displayed. /// There is full support for multi-row deletion. /// Optional manual code can be included to take action prior, during or after each deletion. /// When the row(s) have been deleted the highlighted row index stays the same unless the deleted row was the last one. /// The Details for the newly highlighted row are automatically displayed - or not, if the grid has now become empty. /// </summary> private void DeletePPostcodeRegionRange() { string CompletionMessage = String.Empty; if ((FPreviouslySelectedRangeRow == null) || (FPrevRangeRowChangedRow == -1)) { return; } DataRowView[] HighlightedRows = grdRanges.SelectedDataRowsAsDataRowView; if (HighlightedRows.Length == 1) { TVerificationResultCollection VerificationResults = null; if (TVerificationHelper.IsNullOrOnlyNonCritical(FPetraUtilsObject.VerificationResultCollection)) { this.Cursor = Cursors.WaitCursor; TRemote.MPartner.ReferenceCount.WebConnectors.GetNonCacheableRecordReferenceCount( FMainDS.PPostcodeRegionRange, DataUtilities.GetPKValuesFromDataRow(FPreviouslySelectedRangeRow), FPetraUtilsObject.MaxReferenceCountOnDelete, out VerificationResults); this.Cursor = Cursors.Default; } if ((VerificationResults != null) && (VerificationResults.Count > 0)) { MessageBox.Show(Messages.BuildMessageFromVerificationResult( Catalog.GetString("Record cannot be deleted!") + Environment.NewLine + Catalog.GetPluralString("Reason:", "Reasons:", VerificationResults.Count), VerificationResults), Catalog.GetString("Record Deletion")); return; } string DeletionQuestion = Catalog.GetString("Are you sure you want to delete the current row?"); if ((FPrimaryKeyControl != null) && (FPrimaryKeyLabel != null)) { DeletionQuestion += String.Format("{0}{0}({1} {2})", Environment.NewLine, "Range Name:", FPreviouslySelectedRangeRow.Range); } bool AllowDeletion = true; bool DeletionPerformed = false; if (AllowDeletion) { if ((MessageBox.Show(DeletionQuestion, Catalog.GetString("Confirm Delete"), MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)) { try { FPreviouslySelectedRangeRow.Delete(); DeletionPerformed = true; } catch (Exception ex) { MessageBox.Show(String.Format(Catalog.GetString("An error occurred while deleting this record.{0}{0}{1}"), Environment.NewLine, ex.Message), Catalog.GetString("Error"), MessageBoxButtons.OK, MessageBoxIcon.Warning); } if (DeletionPerformed) { FPetraUtilsObject.SetChangedFlag(); } // Select and display the details of the nearest row to the one previously selected grdRanges.SelectRowInGrid(FPrevRangeRowChangedRow, true); // Clear any errors left over from the deleted row FPetraUtilsObject.VerificationResultCollection.Clear(); } } if (DeletionPerformed && (CompletionMessage.Length > 0)) { MessageBox.Show(CompletionMessage, Catalog.GetString("Deletion Completed")); } } else { string DeletionQuestion = String.Format(Catalog.GetString( "Do you want to delete the {0} highlighted rows?{1}{1}"), HighlightedRows.Length, Environment.NewLine); DeletionQuestion += Catalog.GetString("Each record will be checked to confirm that it can be deleted."); if (MessageBox.Show(DeletionQuestion, Catalog.GetString("Confirm Delete"), MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes) { int recordsDeleted = 0; int recordsUndeletable = 0; int recordsDeleteDisallowed = 0; List <TMultiDeleteResult> listConflicts = new List <TMultiDeleteResult>(); List <TMultiDeleteResult> listExceptions = new List <TMultiDeleteResult>(); this.Cursor = Cursors.WaitCursor; foreach (DataRowView drv in HighlightedRows) { PPostcodeRegionRangeRow rowToDelete = (PPostcodeRegionRangeRow)(drv.Row); string rowDetails = MakePKValuesStringManual(rowToDelete); TVerificationResultCollection VerificationResults = null; TRemote.MPartner.ReferenceCount.WebConnectors.GetNonCacheableRecordReferenceCount( FMainDS.PPostcodeRegionRange, DataUtilities.GetPKValuesFromDataRow(rowToDelete), FPetraUtilsObject.MaxReferenceCountOnDelete, out VerificationResults); if ((VerificationResults != null) && (VerificationResults.Count > 0)) { TMultiDeleteResult result = new TMultiDeleteResult(rowDetails, Messages.BuildMessageFromVerificationResult(String.Empty, VerificationResults)); listConflicts.Add(result); continue; } bool AllowDeletion = true; bool DeletionPerformed = false; if (AllowDeletion) { try { rowToDelete.Delete(); DeletionPerformed = true; } catch (Exception ex) { TMultiDeleteResult result = new TMultiDeleteResult(rowDetails, ex.Message); listExceptions.Add(result); } } else { recordsDeleteDisallowed++; } if (DeletionPerformed) { FPetraUtilsObject.SetChangedFlag(); recordsDeleted++; } } this.Cursor = Cursors.Default; // Select and display the details of the nearest row to the one previously selected grdRanges.SelectRowInGrid(FPrevRangeRowChangedRow, true); if ((recordsDeleted > 0) && (CompletionMessage.Length > 0)) { MessageBox.Show(CompletionMessage, Catalog.GetString("Deletion Completed")); } // Show the results of the multi-deletion string results = null; if (recordsDeleted > 0) { string s1 = Catalog.GetPluralString("record", "records", recordsDeleted); string s2 = Catalog.GetPluralString("was", "were", recordsDeleted); results = String.Format(Catalog.GetString("{0} {1} {2} successfully deleted."), recordsDeleted, s1, s2); } else { results = "No records were deleted."; } if (recordsUndeletable > 0) { string s1 = Catalog.GetPluralString("record", "records", recordsUndeletable); string s2 = Catalog.GetPluralString("it is marked", "they are marked", recordsUndeletable); results += String.Format(Catalog.GetString("{0}{1} {2} could not be deleted because {3} as non-deletable."), Environment.NewLine, recordsUndeletable, s1, s2); } if (recordsDeleteDisallowed > 0) { string s1 = Catalog.GetPluralString("record was not be deleted", "records were not be deleted", recordsUndeletable); results += String.Format(Catalog.GetString("{0}{1} {2} because deletion was not allowed."), Environment.NewLine, recordsDeleteDisallowed, s1); } bool showCancel = false; if (listConflicts.Count > 0) { showCancel = true; string s1 = Catalog.GetPluralString("record", "records", listConflicts.Count); string s2 = Catalog.GetPluralString("it is referenced", "they are referenced", listConflicts.Count); results += String.Format(Catalog.GetString("{0}{1} {2} could not be deleted because {3} by at least one other table."), Environment.NewLine, listConflicts.Count, s1, s2); } if (listExceptions.Count > 0) { showCancel = true; string s1 = Catalog.GetPluralString("record", "records", listExceptions.Count); results += String.Format(Catalog.GetString("{0}{1} {2} could not be deleted because the delete action failed unexpectedly."), Environment.NewLine, listExceptions.Count, s1); } if (showCancel) { results += String.Format(Catalog.GetString("{0}{0}Click OK to review the details, or Cancel to return direct to the data screen"), Environment.NewLine); if (MessageBox.Show(results, Catalog.GetString("Delete Action Summary"), MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.OK) { ReviewMultiDeleteResults(listConflicts, Catalog.GetString("Rows in this table that are referenced by other tables")); ReviewMultiDeleteResults(listExceptions, Catalog.GetString("Unexpected Exceptions")); } } else { MessageBox.Show(results, Catalog.GetString("Delete Action Summary"), MessageBoxButtons.OK, MessageBoxIcon.Information); } } } }
/// <summary> /// The main method of this class. It deletes one or more rows selected in a grid on the caller form /// </summary> /// <param name="ACallerFormOrControl">The form or user control that is making the call. The form must implement the IDeleteGridRows interface</param> /// <param name="AGrid">A reference to the grid object</param> /// <param name="APetraUtilsObject">A reference to the PetraUtilsObject associated with the form or control making the call</param> /// <param name="AButtonPanel">A reference a form or control that implements the IButtonPanel interface. This parameter can be null.</param> /// <returns>True if any rows were actually deleted</returns> public static bool DeleteRows(IDeleteGridRows ACallerFormOrControl, TSgrdDataGrid AGrid, TFrmPetraEditUtils APetraUtilsObject, IButtonPanel AButtonPanel) { DataRow currentDataRow = ACallerFormOrControl.GetSelectedDataRow(); Int32 currentRowIndex = ACallerFormOrControl.GetSelectedRowIndex(); if ((currentDataRow == null) || (currentRowIndex == -1)) { return false; } string CompletionMessage = String.Empty; DataRowView[] HighlightedRows = AGrid.SelectedDataRowsAsDataRowView; if (HighlightedRows.Length == 1) { // Single row deletion TVerificationResultCollection VerificationResults = null; if (TVerificationHelper.IsNullOrOnlyNonCritical(APetraUtilsObject.VerificationResultCollection)) { ACallerFormOrControl.GetReferenceCount(currentDataRow, APetraUtilsObject.MaxReferenceCountOnDelete, out VerificationResults); } if ((VerificationResults != null) && (VerificationResults.Count > 0)) { TCascadingReferenceCountHandler countHandler = new TCascadingReferenceCountHandler(); TFrmExtendedMessageBox.TResult result = countHandler.HandleReferences(APetraUtilsObject, VerificationResults, true); if (result == TFrmExtendedMessageBox.TResult.embrYes) { // repeat the count but with no limit to the number of references ACallerFormOrControl.GetReferenceCount(currentDataRow, 0, out VerificationResults); countHandler.HandleReferences(APetraUtilsObject, VerificationResults, false); } return false; } string DeletionQuestion = ACallerFormOrControl.GetDefaultDeletionQuestion(); bool AllowDeletion = true; bool DeletionPerformed = false; ACallerFormOrControl.HandlePreDelete(currentDataRow, ref AllowDeletion, ref DeletionQuestion); if (AllowDeletion) { if ((MessageBox.Show(DeletionQuestion, MCommonResourcestrings.StrConfirmDeleteTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)) { try { if (!ACallerFormOrControl.HandleDeleteRow(currentDataRow, ref DeletionPerformed, ref CompletionMessage)) { currentDataRow.Delete(); DeletionPerformed = true; } } catch (Exception ex) { MessageBox.Show(String.Format(MCommonResourcestrings.StrErrorWhileDeleting, Environment.NewLine, ex.Message), MCommonResourcestrings.StrGenericError, MessageBoxButtons.OK, MessageBoxIcon.Warning); } if (DeletionPerformed) { APetraUtilsObject.SetChangedFlag(); } // Select and display the details of the nearest row to the one previously selected ACallerFormOrControl.SelectRowInGrid(currentRowIndex); // Clear any errors left over from the deleted row APetraUtilsObject.VerificationResultCollection.Clear(); if (AButtonPanel != null) { AButtonPanel.UpdateRecordNumberDisplay(); } } } if (!ACallerFormOrControl.HandlePostDelete(currentDataRow, AllowDeletion, DeletionPerformed, CompletionMessage)) { if (DeletionPerformed && (CompletionMessage.Length > 0)) { MessageBox.Show(CompletionMessage, MCommonResourcestrings.StrDeletionCompletedTitle); } } return DeletionPerformed; } else { // Multi-row deletion int recordsDeleted = 0; string DeletionQuestion = String.Format(MCommonResourcestrings.StrMultiRowDeletionQuestion, HighlightedRows.Length, Environment.NewLine); DeletionQuestion += MCommonResourcestrings.StrMultiRowDeletionCheck; if (MessageBox.Show(DeletionQuestion, MCommonResourcestrings.StrConfirmDeleteTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes) { int recordsUndeletable = 0; int recordsDeleteDisallowed = 0; List <TMultiDeleteResult>listConflicts = new List <TMultiDeleteResult>(); List <TMultiDeleteResult>listExceptions = new List <TMultiDeleteResult>(); APetraUtilsObject.GetForm().Cursor = Cursors.WaitCursor; foreach (DataRowView drv in HighlightedRows) { DataRow rowToDelete = drv.Row; string rowDetails = MakePKValuesString(rowToDelete); if (!ACallerFormOrControl.IsRowDeletable(rowToDelete)) { recordsUndeletable++; continue; } TVerificationResultCollection VerificationResults = null; ACallerFormOrControl.GetReferenceCount(rowToDelete, APetraUtilsObject.MaxReferenceCountOnDelete, out VerificationResults); if ((VerificationResults != null) && (VerificationResults.Count > 0)) { TMultiDeleteResult result = new TMultiDeleteResult(rowDetails, Messages.BuildMessageFromVerificationResult(String.Empty, VerificationResults)); listConflicts.Add(result); continue; } bool AllowDeletion = true; bool DeletionPerformed = false; ACallerFormOrControl.HandlePreDelete(rowToDelete, ref AllowDeletion, ref DeletionQuestion); if (AllowDeletion) { try { if (!ACallerFormOrControl.HandleDeleteRow(rowToDelete, ref DeletionPerformed, ref CompletionMessage)) { rowToDelete.Delete(); DeletionPerformed = true; } } catch (Exception ex) { TMultiDeleteResult result = new TMultiDeleteResult(rowDetails, ex.Message); listExceptions.Add(result); } } else { recordsDeleteDisallowed++; } if (DeletionPerformed) { APetraUtilsObject.SetChangedFlag(); recordsDeleted++; } ACallerFormOrControl.HandlePostDelete(rowToDelete, AllowDeletion, DeletionPerformed, String.Empty); } APetraUtilsObject.GetForm().Cursor = Cursors.Default; ACallerFormOrControl.SelectRowInGrid(currentRowIndex); if (AButtonPanel != null) { AButtonPanel.UpdateRecordNumberDisplay(); } if ((recordsDeleted > 0) && (CompletionMessage.Length > 0)) { MessageBox.Show(CompletionMessage, MCommonResourcestrings.StrDeletionCompletedTitle); } // Show the results of the multi-deletion string results = null; if (recordsDeleted > 0) { results = String.Format( Catalog.GetPluralString(MCommonResourcestrings.StrRecordSuccessfullyDeleted, MCommonResourcestrings.StrRecordsSuccessfullyDeleted, recordsDeleted), recordsDeleted); } else { results = MCommonResourcestrings.StrNoRecordsWereDeleted; } if (recordsUndeletable > 0) { results += String.Format( Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedBecauseNonDeletable, MCommonResourcestrings.StrRowsNotDeletedBecauseNonDeletable, recordsUndeletable), Environment.NewLine, recordsUndeletable); } if (recordsDeleteDisallowed > 0) { results += String.Format( Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedBecauseDeleteNotAllowed, MCommonResourcestrings.StrRowsNotDeletedBecauseDeleteNotAllowed, recordsDeleteDisallowed), Environment.NewLine, recordsDeleteDisallowed); } bool showCancel = false; if (listConflicts.Count > 0) { showCancel = true; results += String.Format( Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedBecauseReferencedElsewhere, MCommonResourcestrings.StrRowsNotDeletedBecauseReferencedElsewhere, listConflicts.Count), Environment.NewLine, listConflicts.Count); } if (listExceptions.Count > 0) { showCancel = true; results += String.Format( Catalog.GetPluralString(MCommonResourcestrings.StrRowNotDeletedDueToUnexpectedException, MCommonResourcestrings.StrRowNotDeletedDueToUnexpectedException, listExceptions.Count), Environment.NewLine, listExceptions.Count); } if (showCancel) { results += String.Format(MCommonResourcestrings.StrClickToReviewDeletionOrCancel, Environment.NewLine); if (MessageBox.Show(results, MCommonResourcestrings.StrDeleteActionSummaryTitle, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.OK) { ReviewMultiDeleteResults(listConflicts, MCommonResourcestrings.StrRowsReferencedByOtherTables); ReviewMultiDeleteResults(listExceptions, MCommonResourcestrings.StrExceptions); } } else { MessageBox.Show(results, MCommonResourcestrings.StrDeleteActionSummaryTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); } } return recordsDeleted > 0; } }