
Untitled
By: a guest on
Jun 22nd, 2012 | syntax:
None | size: 0.82 KB | hits: 8 | expires: Never
Can we make the below Datatable subtraction method as generic
private DataTable DtSubtract(DataTable dt, int insertAt)
{
DataRow dr;
List<object> lstColumnSummation = new List<object>();
double res = 0;
for (int i = 0; i < dt.Columns.Count; i++)
{
for (int row = 0; row < dt.Rows.Count; row++)
res = Convert.ToDouble(dt.Rows[row][i]) - res;
lstColumnSummation.Add(-(res));
res = 0;
}
//add a new data row
dr = dt.NewRow();
dr.ItemArray = lstColumnSummation.ToArray();
//insert the row at the position specified
dt.Rows.InsertAt(dr, insertAt);
//accept the changes
dt.AcceptChanges();
return dt;
}
First is: double res = 0; TO int res = 0;
Second is: res = Convert.ToDouble(dt.Rows[row][i]) - res; TO res = Convert.ToInt32(dt.Rows[row][i]) - res;