【发布时间】: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