Reputation: 69
I am computing in the large datatable. The table name is templetable.
I am trying to calculate the sum from the 4th column to the last column and save the results to another datatable. But I am getting errors as it says could not find the column "colPnow"
Any ideas?
DataView dv2 = new DataView(SP_dt);
for (int i = 0; i < LoadIDcount; i++)
{
string IDnow = LoadID[i, 0];
dv2.RowFilter = String.Format("Load_ID = '{0}'", IDnow);
DataTable temptable = dv2.ToTable();
for (int j = 0; j < 8760; j++)
{
string colPnow = SP_dt.Columns[j*2 + 4].ColumnName.ToString();
double ColP_sum= (double)temptable.Compute("Sum(colPnow)", String.Format("Load_ID = '{0}'", IDnow));
string colQnow = SP_dt.Columns[j*2 + 5].ColumnName.ToString();
double ColQ_sum = (double)temptable.Compute("Sum(colQnow)", String.Format("Load_ID = '{0}'", IDnow));
Load_dt.Rows[i][j * 2 + 2] = ColP_sum;
Load_dt.Rows[i][j * 2 + 3] = ColQ_sum;
}
}
Upvotes: 2
Views: 218
Reputation: 646
You are not formatting your expression string correctly in the Compute
method. Use another String.Format
and it will work. Your Compute
expression is currently literally equal to "Sum(colPnow)"
. You want the actual column name string that is stored in your colPnow
variable.
Change this part of your code to this:
string colPnow = SP_dt.Columns[j*2 + 4].ColumnName.ToString();
double ColP_sum= (double)temptable.Compute(String.Format("Sum([{0}])", colPnow),
String.Format("Load_ID = '{0}'", IDnow));
The same should be done for your colQnow
variable. I would also suggest you read how String.Format
works so that you can grasp the string formatting concept.
Upvotes: 2