/// <summary> /// Confirm the deletion of a row from the data source /// </summary> /// <param name="s">The sender of the event</param> /// <param name="e">The event arguments</param> private void dnNav_DeletingRow(object sender, DataNavigatorCancelEventArgs e) { epErrors.Clear(); if(MessageBox.Show(String.Format("Are you sure you want to delete the name '{0} {1}'?", txtFName.Text, txtLName.Text), "Relationship Test", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No) e.Cancel = true; }
/// <summary> /// This raises the <see cref="CancelingEdits"/> event /// </summary> /// <param name="e">The event arguments</param> protected internal virtual void OnCancelingEdits(DataNavigatorCancelEventArgs e) { var handler = CancelingEdits; if(handler != null) handler(this, e); }
/// <summary> /// This raises the <see cref="DeletingRow"/> event /// </summary> /// <param name="e">The event arguments</param> protected virtual void OnDeletingRow(DataNavigatorCancelEventArgs e) { var handler = DeletingRow; if(handler != null) handler(this, e); }
/// <summary> /// This can be used as an alternative to the <c>Delete</c> button to delete a row from the data source /// </summary> /// <param name="delRow">The row to delete</param> /// <returns>True if the row was deleted, false if not deleted due to cancellation</returns> /// <remarks>This is equivalent to clicking the <c>Delete</c> button but it allows you to put a button on /// the parent form or to handle the delete operation in some other fashion. The <see cref="DeletingRow"/> /// event is fired prior to deleting the specified row. If the <c>DeletingRow</c> event is not canceled, /// the specified row is deleted and the <see cref="DeletedRow"/> event is fired.</remarks> /// <exception cref="NotSupportedException">This is thrown if deletions are not currently allowed as /// defined by the current <see cref="AllowDeletes"/> property setting.</exception> /// <exception cref="ArgumentOutOfRangeException">This is thrown if the specified row is outside the /// bounds of the list or if there is no data source.</exception> public bool DeleteRow(int delRow) { if(!changePolicy.AllowDeletes) throw new NotSupportedException(LR.GetString("ExDeleteNotAllowed")); if(delRow < 0 || listManager == null || delRow >= listManager.Count) throw new ArgumentOutOfRangeException("delRow", delRow, LR.GetString("ExInvalidRowNumber")); // Nested deletes aren't supported. if(inDelRow) return false; try { inDelRow = true; // See if the user will allow the delete to occur DataNavigatorCancelEventArgs ce = new DataNavigatorCancelEventArgs(delRow); OnDeletingRow(ce); // Ignore the request if canceled if(ce.Cancel) return false; // If deleting the current row, cancel any pending changes if(delRow == currentRow) this.CancelChanges(); // Delete the row from the data source if it is there if(delRow < listManager.Count) listManager.RemoveAt(delRow); } finally { inDelRow = false; } lblRowCount.Text = LR.GetString("DLNavRowCount", listManager.Count); // Force the focus to the proper row currentRow = -1; DataSource_PositionChanged(this, EventArgs.Empty); OnDeletedRow(new DataNavigatorEventArgs(-1)); btnDelete.Enabled = (changePolicy.AllowDeletes && listManager.Count > 0); return true; }
/// <summary> /// This is used to add a row to the data source /// </summary> /// <returns>True if the row was added, false if it was not due to validation failure or cancellation</returns> private bool AddRowInternal() { if(!changePolicy.AllowAdditions) throw new NotSupportedException(LR.GetString("ExAddNotAllowed")); if(!this.IsValid) return false; try { inAddRow = true; listManager.EndCurrentEdit(); // See if the user wants to allow the addition DataNavigatorCancelEventArgs ce = new DataNavigatorCancelEventArgs(-1); OnAddingRow(ce); if(ce.Cancel) return false; listManager.AddNew(); } finally { inAddRow = false; } lblRowCount.Text = LR.GetString("DLNavRowCount", listManager.Count); currentRow = -1; DataSource_PositionChanged(this, EventArgs.Empty); btnDelete.Enabled = changePolicy.AllowDeletes; OnAddedRow(new DataNavigatorEventArgs(currentRow)); return true; }
/// <summary> /// This handles the <c>KeyDown</c> event in the parent form and processes our shortcut keys /// </summary> private void Parent_KeyDown(object sender, KeyEventArgs e) { // Don't do anything if we are nested inside another control and it doesn't contain the focus if(sender == this.ParentForm && (this.Parent == this.ParentForm || this.Parent.ContainsFocus)) { if(e.KeyData == Keys.Escape && listManager != null && listManager.Count > 0) { // Cancel changes to the current row. Row may not be the same afterwards if canceling changes // on the new row. int row = currentRow; DataNavigatorCancelEventArgs ce = new DataNavigatorCancelEventArgs(row); OnCancelingEdits(ce); if(!ce.Cancel) { this.CancelChanges(); // If the current row changed, pass -1 to indicate that the old row went away if(currentRow != row) OnCanceledEdits(new DataNavigatorEventArgs(-1)); else OnCanceledEdits(new DataNavigatorEventArgs(row)); } } else if(e.KeyData == (Keys)shortcutRowNum) txtRowNum.Focus(); // Set focus to row number text box else if(e.KeyData == (Keys)shortcutAdd && changePolicy.AllowAdditions && listManager != null) this.MoveTo(RowPosition.NewRow); // Add a row else if(e.KeyData == (Keys)shortcutDel && changePolicy.AllowDeletes && listManager != null && listManager.Count != 0) this.DeleteRow(currentRow); // Delete current row } }