private void GetDetailDataFromControlsManual(ACorporateExchangeRateRow ARow)
        {
            // only do this is the user has actually changed the exchange rate
            if ((ARow.RowState != DataRowState.Added) &&
                (Convert.ToDecimal(ARow[ACorporateExchangeRateTable.GetRateOfExchangeDBName(), DataRowVersion.Original]) != ARow.RateOfExchange))
            {
                // Check if we have an inverse rate for this date/time and currency pair
                ACorporateExchangeRateRow mainRow = (ACorporateExchangeRateRow)FMainDS.ACorporateExchangeRate.Rows.Find(
                    new object[] { ARow.ToCurrencyCode, ARow.FromCurrencyCode, ARow.DateEffectiveFrom });

                if ((mainRow != null) && (ARow.RateOfExchange != 0.0m))
                {
                    // Checking to see if we have a matching rate is tricky because rounding errors mean that the inverse of an inverse
                    // does not always get you back where you started.  So we check both ways to look for a match.
                    // If neither way matches we need to do an update, but if there is a match in at least one direction, we leave the other row as it is.
                    decimal inverseRate    = Math.Round(1 / ARow.RateOfExchange, 10);
                    decimal inverseRateAlt = Math.Round(1 / mainRow.RateOfExchange, 10);

                    if ((mainRow.RateOfExchange != inverseRate) && (ARow.RateOfExchange != inverseRateAlt))
                    {
                        // Neither way matches so we must have made a change that requires an update to the inverse row
                        mainRow.BeginEdit();
                        mainRow.RateOfExchange = inverseRate;
                        mainRow.EndEdit();
                    }
                }
            }
        }
Beispiel #2
0
        void FPetraUtilsObject_DataSavingStarted(object Sender, EventArgs e)
        {
            // The user has clicked Save.  We need to consider if we need to make any Inverse currency additions...
            // We need to update the details and validate them first
            // When we return from this method the standard code will do the validation again and might not allow the save to go ahead
            FPetraUtilsObject.VerificationResultCollection.Clear();
            ValidateAllData(false, false);

            if (!TVerificationHelper.IsNullOrOnlyNonCritical(FPetraUtilsObject.VerificationResultCollection))
            {
                return;
            }

            // Now go through all the grid rows (view) checking all the added rows.  Keep a list of inverses
            List <tInverseItem> lstInverses = new List <tInverseItem>();
            DataView            gridView    = ((DevAge.ComponentModel.BoundDataView)grdDetails.DataSource).DataView;

            for (int i = 0; i < gridView.Count; i++)
            {
                ACorporateExchangeRateRow ARow = (ACorporateExchangeRateRow)gridView[i].Row;

                if (ARow.RowState == DataRowState.Added)
                {
                    tInverseItem item = new tInverseItem();
                    item.FromCurrencyCode = ARow.ToCurrencyCode;
                    item.ToCurrencyCode   = ARow.FromCurrencyCode;
                    item.RateOfExchange   = Math.Round(1 / ARow.RateOfExchange, 10);
                    item.DateEffective    = ARow.DateEffectiveFrom;
                    lstInverses.Add(item);
                }
            }

            if (lstInverses.Count == 0)
            {
                return;
            }

            // Now go through our list and check if any items need adding to the data Table
            // The user may already have put an inverse currency in by hand
            DataView dv = new DataView(FMainDS.ACorporateExchangeRate);

            for (int i = 0; i < lstInverses.Count; i++)
            {
                tInverseItem item = lstInverses[i];

                // Does the item exist already?
                dv.RowFilter = String.Format(CultureInfo.InvariantCulture, "{0}='{1}' AND {2}='{3}' AND {4}=#{5}# AND {6}={7}",
                                             ACorporateExchangeRateTable.GetFromCurrencyCodeDBName(),
                                             item.FromCurrencyCode,
                                             ACorporateExchangeRateTable.GetToCurrencyCodeDBName(),
                                             item.ToCurrencyCode,
                                             ACorporateExchangeRateTable.GetDateEffectiveFromDBName(),
                                             item.DateEffective.ToString("d", CultureInfo.InvariantCulture),
                                             ACorporateExchangeRateTable.GetRateOfExchangeDBName(),
                                             item.RateOfExchange);

                if (dv.Count == 0)
                {
                    ACorporateExchangeRateRow NewRow = FMainDS.ACorporateExchangeRate.NewRowTyped();
                    NewRow.FromCurrencyCode  = item.FromCurrencyCode;
                    NewRow.ToCurrencyCode    = item.ToCurrencyCode;
                    NewRow.DateEffectiveFrom = DateTime.Parse(item.DateEffective.ToLongDateString());
                    NewRow.RateOfExchange    = item.RateOfExchange;

                    FMainDS.ACorporateExchangeRate.Rows.Add(NewRow);
                }
            }

            // Now make sure to select the row that was currently selected when we started the Save operation
            SelectRowInGrid(grdDetails.DataSourceRowToIndex2(FPreviouslySelectedDetailRow) + 1);
        }