I have a DataTable (i.e. DT) that has a few columns of Type of String (i.e. colStr1, colStr2), and a few of the Type of Int32 (i.e. colInt1, colInt2). This is set in the DB as well as in the DataTable created in the code.
For one of my functions, I need to get a Sum of the Int column. But for specific data, the Sum will exceed the Int32.MaxValue.
What I'm looking for are two options:
(not sure this one is possible) Add a row, that will contain the SUM of the column while changing the Type for the SUM cell/field from Int32 to Int64.
Add a copy of those Int32 columns (with all the values) to the same DT, but a new column is defined as Type of Int64 (and data converted accordingly). Then I can do a SUM for those. I have following code for this, but not sure if it is most elegant and best way to do it:
DataColumn colBal64 = new DataColumn(); colBal64.ColumnName = "BalanceInt64"; colBal64.DataType = typeof(Int64); dt.Columns.Add(colBal64); for (int i = 0; i <= dt.Rows.Count - 1; i++) { DataRow dr = dt.Rows[i]; dr["BalanceInt64"] = dr["BalanceInt32"]; }
Any suggestion on how to better approach this? And the best way to do it? So far I found only copying the whole DT into a new DT, and not Column within the same DT, while converting into another Type.