Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize DataColumn.Expression handling in C#

I would like to change behavior of DataColumn.Expression so that when I write:

DataColumn.Expression = "MyMethod(Price)"

It will call MyMethod, pass value from Price column into it and display evaluated value.

How can I acomplish this?

like image 907
Tom Smykowski Avatar asked Sep 17 '25 02:09

Tom Smykowski


2 Answers

Not possible. The expression parser behind the Expression property is simple and not extensible. Making arbitrary function calls is not one of its capabilities. There are several ways to work around this, especially ones that don't require an expensive reflection lookup. Consider the DataTable.RowChanged event for example.

like image 144
Hans Passant Avatar answered Sep 19 '25 16:09

Hans Passant


A way around this is to make your function a calculated field like in databases and spredsheets. It is possible to make advanced calculated fields. You need to rewrite your function to an expression and then use [Price] to refer to the source column. As I understand your question it makes it possible to do what you intend. The syntax for expression can be found here.

I know this is a late answer to the question but it might help others looking into how to do this.

like image 24
Lars Buch-Jepsen Avatar answered Sep 19 '25 17:09

Lars Buch-Jepsen