【问题标题】:Error while performing compute SUM() in a column在列中执行计算 SUM() 时出错
【发布时间】:2019-05-05 13:46:24
【问题描述】:

我试图在我的数据网格中计算一列,我将在我的代码中显示这些,如下所示。我不断收到这些错误。

我已经浏览了这些链接
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));

发生错误

聚合函数 Sum() 和类型: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'))", ""));

发生错误

聚合参数中的语法错误:需要一个可能带有“子”限定符的列参数。

我将如何让计算和函数工作?

【问题讨论】:

  • 你在最后一个表达式中有一个错字:你的意思是写MITTRA.Compute("SUM(Convert(Min_Weight,'System.Decimal'))", "")而不是MITTRA.Compute("SUM(Convert(Min_Weight),'System.Decimal')", "")
  • 我稍后会尝试更新你,谢谢Bizhan
  • @Bizhan 感谢您的纠正,但我仍然收到可能的“儿童”限定符错误。
  • 尝试在常量中使用十进制,例如 10,可以写成 10.000 或 10.000M。您还应该以正确的格式定义列,而不是像这样进行所有这些转换:DataColumn colDecimal = new DataColumn("DecimalCol"); colDecimal.DataType = System.Type.GetType("System.Decimal"); myTable.Columns.Add(colDecimal);
  • @NoChance 好的,我会在短时间内尝试,并会及时通知您。谢谢

标签: c# wpf datatable wpfdatagrid


【解决方案1】:

感谢@NoChance 提供的解决方案。希望这篇文章也能对其他人有所帮助。

/***** 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(); 

【讨论】:

  • 通常当您发布自己的解决方案时,您会将其标记为“解决方案”,这样其他人就会知道您很高兴,并且不会再尝试解决问题。
  • @NoChance 是的,我知道这一点,但我还不能标记它,有 2 天的时间缓冲才能接受它作为“解决方案”(绿色勾号),如果我错了。
猜你喜欢
  • 2013-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 1970-01-01
  • 2010-10-26
相关资源
最近更新 更多