custom function for DataColumn Expression RRS feed

  • Question

  • Hi,

    I want to fill my DataColumn based on more than one other column in the same DataTable, like

    Column.Expression =" Function1(col1,col2)"

    the solution presented here specifies that it's possible to achieve this but with only one dependent column like this :

    Column.Expression = "col1";

    and then using the DataGridView_CellFormatting event to apply the Function on the returned value, I was wondering how to use this soluyion with multiple columns or is there another way to achieve this perhaps without needing the dataGridView.

    Thank you.
    Monday, February 26, 2018 3:13 PM

All replies

  • I believe an decent way to work with this is not with expressions but instead Binding class.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, February 26, 2018 3:18 PM
  • Thank you for your response, but I didn't understand how the binding will help me in this case, can you give me an example to better understand?

    Thank you.

    Monday, February 26, 2018 3:54 PM
  • Usseer, 

    That link is from 2009, the time there was no Linq (or just some days :-) ). The expressions are very limited, one of the reasons Linq was created. 

    However, what you show is not an expression of a column. The use of an expession  can be that you add an extra column to the table which does not exist in the database (and will not be created there) with for instance 

    MyTable.Colums.Add(new DataColumn);

    Column.Expression = "(col1 + col2) * col3";


    Monday, February 26, 2018 4:18 PM
  • @Cor The first expression I provided is my goal but I know that it does not work, I just put it there to explain my purpose. The second one is actually proposed as a solution alongside the use of an event to apply some function on that value in the link I provided.

    How is Linq going to help me to achieve my purpose? Which is applying a complex function( not only + * / ) on my data for each row in the data table?


    Monday, February 26, 2018 4:36 PM
  • But the second one is just a copy of col1, be aware that for an expression you need an extra datacolumn.

    To be honnest, I used the DataTable until Linq started and know the Expression well. But I know that there is Linq to DataSets which should replace the expression.


    Monday, February 26, 2018 4:46 PM