2

I need to enable user that he can write own formula in datagridview. Something like a function in Excel.

Example of formula definition:

x

So, user write his own formula in formula cell and then in other table is shown result for each. How I can do this?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
user1320251
  • 21
  • 1
  • 2

5 Answers5

2

I would try NCalc

NCalc is a mathematical expressions evaluator in .NET. NCalc can parse any expression and evaluate the result, including static or dynamic parameters and custom functions.

Dictionary<string, int> dict = new Dictionary<string, int>() { { "Income", 1000 }, { "Tax", 5 } };

string expressionString = "Income * Tax";
NCalc.Expression expr = new NCalc.Expression(expressionString);
expr.EvaluateParameter += (name, args) =>
    {
        args.Result = dict[name];
    };

int result = (int)expr.Evaluate();
L.B
  • 114,136
  • 19
  • 178
  • 224
  • I take a look on all solution and at the moment NCals is maybe the most simple solution for my problem. Thanks to everybody for help. – user1320251 Apr 08 '12 at 13:50
0

Your formula could be manipulated to C# and dynamically compiled using SystemCodeCom.Compiler and you could run it on the fly feeding in your variable values.
Otherwise you are going to have to impliment some kind of mini parser/compiler - which is a rather particular skill and which could quickly get complicated - especially if your formulas become more complicated (which maybe likely).
There is are codeproject articles on dynamic complilation here and here. But there are plenty of other examples around on the web.

Ricibob
  • 7,505
  • 5
  • 46
  • 65
  • Tnx! I will try with dynamic complilation. – user1320251 Apr 08 '12 at 13:05
  • In my opinion, dynamic compilation (as it is done in the average examples on the web) is rather bad, since every new compiled item adds to the app domain and cannot be unloaded anymore. The better approaches create their own app domain but the is a lot of overhead when communicating with the dynamic compiled code. For formulas, I use a formular parser like e.g. [this one](http://www.codeproject.com/Articles/7335/An-extensible-math-expression-parser-with-plug-ins). – Uwe Keim Apr 08 '12 at 13:09
  • 1
    @UweKeim Hm that looks quite nice - probably worthy of a separate answer! – Ricibob Apr 08 '12 at 13:12
0

There are a number of ways you can do that, they all revolve around translating the formula into executable code. Do you want to write your own parser or do you wnat to use an existing one. C# itself, IronPython, IronRuby, some off the shelf component. If you use a full parser you might want to look at how to restrict what the user can do with it, inadvertantly or otherwise...

If they are as simple as they look, some sort of expression builder, (pick two named values and an operator) might be the way to go, but modularise, both building the expression and evaluating it so you can beef up at some later point.

However given how simple they appear to be, I'd be tempted to predefine expressions (loaded as meta data from some sort of backing store, and make it select one of these as opposed to user entering it. You could easily spend months at this aspect of the design, is it worth it?

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
0

I had a similar requirement (dynamic parsing of expressions) recently in a project I am working on and ended up using VB expressions from WF (Windows Workflow Foundation). It certainly depends on how important this functionality is for you and how much effort are you willing to put into it. In my case it turned out better than NCalc for several reasons:

  • it supports more complex expressions than NCalc
  • the resulting expression trees can be analyzed to determine dependencies for individual expressions
  • it's the same language for expressions as in WF (which I already use elsewhere in the project)

Anyway, here is a short blogpost I've written on the subject.

Damir Arh
  • 17,637
  • 2
  • 45
  • 83
0

I created the albatross expression parser a few years back. It has been open sourced for a while but I finally get around and published v2.02 and added documentation recently. It is being actively maintained. It has a couple nice features:

  • Circular reference detection
  • Source variable from external object directly
  • Reversed generation of expressions
  • Properly documented
Rushui Guan
  • 3,023
  • 1
  • 24
  • 26