none
How to customize calculated DataColumn? RRS feed

  • Question

  •  

    Hi all. Calculated DataColumn is a wanderfu idea, but I have more requirement as below: given the two data column A and B, it's required to define another calculated column C which type is D; 1) when getting data from C, it returns an instance of { new D (A, B) } and 2) when setting to column C, table also updates A and B.

     

    For the 1st requirement, I know it has the Expression property, but it seems to me that it only support simple calcualtion such as concat strings and built-in expression. Is it a way to add my own Function such as Convert?

     

    For the both requirement, is there any events before & after I getting/setting data from/to a DataColumn? If yes, I can do the convertion all by myself?

     

    Sunday, September 30, 2007 11:16 AM

All replies

  • 1) You cannot use your own function, but calculated columns allow using built-in CONVERT function. It might be sufficient in your case.

     

    2) DataTable has two events called ColumnChanging and ColumnChanged. I believe this is what you need.

     

    Sunday, September 30, 2007 12:35 PM
    Moderator
  •  

    1) You cannot use your own function, but calculated columns allow using built-in CONVERT function. It might be sufficient in your case.

     

    >>> I checked MSDN, find an example of Convert, "Convert(total, 'System.Int32')". So my understanding is that it can convert total to Int32, but my requirement is to convert A and B to a customized type by using the ctor. I didn't find a way to do this, could you please give me example? :-)

     

    2) DataTable has two events called ColumnChanging and ColumnChanged. I believe this is what you need.

     

    >>> Yup... I can use them when setting value to the calcualted column, but a problem is if I set Expression for the column C, it becomes ReadOnly and throws exception when setting value to it. It seems that I have to customize the calcualte feature all by myself instead of using built-in Expression, correct? If yes, a problem is which event should I use when Getting data from C?

    Sunday, September 30, 2007 2:04 PM
  • 1) Custom types would not work with CONVERT function in this case

     

    2) Yes, using events probably would solve all you need. Which one to use is up to you. The difference is that Changing event happens when row is not committed yet to the DataTable, but changed happens afterwards. There are also two similar events on a row level: RowChanging and RowChanged

     

    Sunday, September 30, 2007 8:31 PM
    Moderator