We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Problem with % in Formulas

Hello,

I am on 5.1 final.

The formula =SUM($H$1:H15)*C17% which I am tracking via a CalcEngine and displaying in a GridControl is throwing an "invalid expression" error. Entering the same formula directly in a GridControl with FormulaCells

VirtGridMultipleDataSources0.zip

4 Replies

AD Administrator Syncfusion Team July 2, 2007 07:33 AM UTC

I think your post might have been truncated for some reason. And the sample does not show any use of Calculate. Could you repost your question and explain how to see the problem in the sample you uploaded?

I did paste the formula you provided in our \Windows\Calculate.Windows\Samples\2.0\GridDataBoundGridCalculator sample and it does not work there. The reason is that currently Essential Calculate does not recognize the % as a unary operator. The only unary operator recognized by Essential Calculate is the unary minus. Depending upon your exact use case, you may be able to tweak your ICalcData implemetation to handle (or at least avoid the error from) a unary % operator. If you post a sample showing your use case, maybe we can suggest something.


BF Ben Fraser July 2, 2007 11:37 PM UTC

My apologies, the truncation may be due to trying out Safari on Windows but attaching the wrong example was my fault.

If you open this example and enter =SUM($H$1:H15)*C17% into a cell you will see the error.

Avoiding the problem isn't really an option, we want Excel formula compatibility.

I assume from your reply that GridControls use a specialise CalcEngine in the background? Is it possible to access that one in my code? Can you point me to the location in the source where this is implemented?

Thanks,
Ben

VirtGridMultipleDataSources1.zip


AD Administrator Syncfusion Team July 3, 2007 09:21 AM UTC

Below is your sample back with an example of what I had in mind when I susgested tweaking the ICalcData implementation. I added an Engine reference to ArrayCalcData and tweak the code in SetValueRowCol to handle the %, turing it to a / 100 so the computed values will reflect the entered % formula. This may not serve your needs though as it steps on the actual formula being set.

The CalcEngine class in Essential Calculate and the GridFormulaEngine class in Essential Grid are not directed related as derived classes but they do share a significant amount of code. In general, if you are using grid classes, then using the GridFormulaEngine is a better fit (though when using virtual GridControls or GridDataBoundGrids, you lose some of the direct support of maintaining the style.FormulatTags - but generally this can be handled via QueryCellInfo and SaveCellInfo).

I am not sure exactly what featues of a GridFormulaEngine you need, but given a string starting with an =, you can parse it using engine.Parse. You can then compute the parsed formula with engine.ComputedValue. To get a reference to the GridFormulaEngine associated with a GridControl, you use

GridFormulaEngine engine = ((GridFormulaCellModel)grid.CellsModels["FormulaCell"]).Engine;


To get at a particular cells' formula, you use grid[row, col].Text. To get at a particular cell's computed value, you can use grid[row, col].FormattedText. The engine.IsFormulaValid method exposes ways of testing formula syntax in grid.CurrentCellValidating (for example).

If this is not the information you need on using the GridFormulaEngine, please ask further questions on it.


AD Administrator Syncfusion Team July 3, 2007 09:23 AM UTC

I forgot to upload the modified sample. Here it is.


VirtGridMultipleDataSources2.zip

Loader.
Live Chat Icon For mobile
Up arrow icon