private void FocusedRangeRowChanged(System.Object sender, SourceGrid.RowEventArgs e) { DataRowView rowView = (DataRowView)grdRanges.Rows.IndexToDataSourceRow(e.Row); if (rowView != null) { FPreviouslySelectedRangeRow = (PPostcodeRegionRangeRow)(rowView.Row); } FPrevRangeRowChangedRow = e.Row; btnRemove.Enabled = true; }
private string MakePKValuesStringManual(PPostcodeRegionRangeRow ARow) { string ReturnValue = String.Empty; object[] items = DataUtilities.GetPKValuesFromDataRow(ARow); for (int i = 0; i < items.Length; i++) { if (i > 0) { ReturnValue += ", "; } ReturnValue += items[i].ToString(); } return(ReturnValue); }
/// <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); } } } }
private string MakePKValuesStringManual(PPostcodeRegionRangeRow ARow) { string ReturnValue = String.Empty; object[] items = DataUtilities.GetPKValuesFromDataRow(ARow); for (int i = 0; i < items.Length; i++) { if (i > 0) { ReturnValue += ", "; } ReturnValue += items[i].ToString(); } return ReturnValue; }