在列中执行计算 SUM() 时出错

Error while performing compute SUM() in a column

我已经尝试计算我的数据网格中的一列,我将按照下面的代码在我的代码中显示这些列。我不断收到这些错误。

我浏览了这些链接
1. How To Convert The DataTable Column type?
2. Error while taking SUM() of column in datatable
3.Invalid usage of aggregate function Sum() and Type: String

//This is the column i add into my datagrid
MITTRA.Columns.Add("Min_Tol");
MITTRA.Columns.Add("Max_Tol");
MITTRA.Columns.Add("Min_Weight");
MITTRA.Columns.Add("Max_Weight");

// The following codes is working finely
// if you notice MTTRQT column, it's a data queried from database
for (int i = 0; i <= MITTRA.Rows.Count - 1; i++)
{
    string item = MITTRA.Rows[i]["MTITNO"].ToString();

    Tolerancechecking = database_select4.LoadUser_Tolerance(item);

    MITTRA.Rows[i]["Min_Tol"] = Tolerancechecking.Rows[0]["Min_Tol"].ToString();
    MITTRA.Rows[i]["Max_Tol"] = Tolerancechecking.Rows[0]["Max_Tol"].ToString();

    MITTRA.Rows[i]["Min_Weight"] = Convert.ToDecimal(MITTRA.Rows[i]["MTTRQT"]) - ((Convert.ToDecimal(MITTRA.Rows[i]["MTTRQT"]) * Convert.ToDecimal(MITTRA.Rows[i]["Min_Tol"]) / 10));
    MITTRA.Rows[i]["Max_Weight"] = Convert.ToDecimal(MITTRA.Rows[i]["MTTRQT"]) + ((Convert.ToDecimal(MITTRA.Rows[i]["MTTRQT"]) * Convert.ToDecimal(MITTRA.Rows[i]["Max_Tol"]) / 10));

    dataGrid2.Columns.Clear();
    dataGrid2.ItemsSource = null;
    dataGrid2.ItemsSource = Tolerancechecking.DefaultView;

}


//Working Sum computation
Decimal Sum = Convert.ToDecimal(MITTRA.Compute("SUM(MTTRQT)", string.Empty));  
MaxTol.Text = Sum.ToString();  /*** This is working as i got my value on the text box ****/

尝试对不同列进行总和计算时出错 初步尝试

  1. Decimal Sum = Convert.ToDecimal(MITTRA.Compute("SUM(Min_Weight)", string.Empty));

出现错误

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

第二次尝试

  2.  Decimal Sum = Convert.ToDecimal(MITTRA.Compute("SUM(Convert(Min_Weight,'System.Decimal'))", string.Empty));
  3.  Decimal Sum = Convert.ToDecimal(MITTRA.Compute("SUM(Convert(Min_Weight,'System.Decimal'))", ""));

出现错误

Syntax error in aggregate argument: Expecting a single column argument with possible 'Child' qualifier.

我怎样才能让计算总和函数起作用?

感谢@NoChance 提供的解决方案。希望这个post也能帮助到其他人。

/***** This is the part where i declare my datacolumn data type ****/
DataColumn Min_Weight = new DataColumn("Min_Weight");
Min_Weight.DataType = System.Type.GetType("System.Decimal");
MITTRA.Columns.Add(Min_Weight);

/**** This Works finally *****/ 
Decimal Sum = Convert.ToDecimal(MITTRA.Compute("SUM(Min_Weight)", string.Empty)); 
MaxTol.Text = Sum.ToString();