/// <summary> /// Deliberately adds BO to the grid's collection because current habanero model only /// adds BO to grid when it is saved. This causes a problem when you call col.CreateBO(), since /// it adds the BO twice and throws a duplicate key exception. /// </summary>a private void NewButtonClicked(object sender, EventArgs e) { _newBO = null; IBusinessObject currentBO = CurrentBusinessObject; if (currentBO != null) { if (!currentBO.IsValid()) { _iboEditorControl.DisplayErrors(); return; } currentBO.Save(); } IBusinessObjectCollection collection = _readOnlyGridControl.Grid.BusinessObjectCollection; _readOnlyGridControl.Grid.SelectionChanged -= GridSelectionChanged; _newBO = collection.CreateBusinessObject(); // _readOnlyGridControl.Grid.GetBusinessObjectCollection().Add(businessObject); _readOnlyGridControl.SelectedBusinessObject = _newBO; CurrentBusinessObject = _newBO; UpdateControlEnabledState(); //collection.Add(businessObject); _readOnlyGridControl.Grid.SelectionChanged += GridSelectionChanged; GridSelectionChanged(null, null); _iboEditorControl.Focus(); _iboEditorControl.ClearErrors(); _cancelButton.Enabled = true; }
private void GridSelectionChanged(object sender, EventArgs e) { if (_newBO != null) { if (_newBO.Status.IsDirty) { if (_newBO.IsValid()) { _newBO.Save(); } else { _newBO = null; } } } if (_lastSelectedBusinessObject == null || _readOnlyGridControl.SelectedBusinessObject != _lastSelectedBusinessObject) { if (!CheckRowSelectionCanChange()) { return; } SavePreviousBusinessObject(); SetSelectedBusinessObject(); } }
/// <summary> /// Using the RowValidating event did not work as expected, so this method provides /// a way to check whether the grid selection should be forced back to the previous selection /// </summary> private bool CheckRowSelectionCanChange() { if (_lastSelectedBusinessObject != null && _readOnlyGridControl.SelectedBusinessObject != _lastSelectedBusinessObject) { if (!_lastSelectedBusinessObject.IsValid()) { _iboEditorControl.DisplayErrors(); _readOnlyGridControl.SelectedBusinessObject = _lastSelectedBusinessObject; return(false); } } return(true); }