Search code examples
c#datatablecolumn-types

How To Convert The DataTable Column type?


I am trying to implement:

dtGroupedBy.Compute("sum(POWER)",dvv.RowFilter); 

It results of an error:

Invalid usage of aggregate function Sum() and Type: String.

How would we convert it to its column type ??


Solution

  • Compute method supports expressions and they support the CONVERT function that you could use inside of the expression.

    For example, you could calculate sum of the column like

    MyDataTable.Compute("Sum(Convert(your column name, 'Data Type'))")