当我在datagrid中对datagrid中的列进行求和时,我很难 [英] I have difficult when I make sum of a column in datagrid also in datatable

查看:62
本文介绍了当我在datagrid中对datagrid中的列进行求和时,我很难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在datagrid中对数据表中的列进行求和时,我很困难

i在数据表中有4列,当我在列Qte中进行求和时,它正常(整数类型)
b栏(双人型)
它有错误



请帮助



我尝试过:



点心为双倍

sum = dt .Compute(SUM(PU),)

MsgBox(sum)

解决方案

首先回到您的数据源并查看您存储的原始数据:错误消息清楚地说明您不能对字符串使用聚合函数。

这意味着无论PU持有什么数据,它都存储为字符串,而不是数字 - 您需要将其转换为数字以便对值进行求和。



I强烈建议您从原始数据开始,如果是数据库,则更改它:始终将值存储在a中适当的数据类型列:字符串仅用于基于字符串的数据,如名称,地址,移动电话,电子邮件等...日期应在DateTime中,数字应为int,float,double或decimal。

所有的字符串看起来都很简单,但是后来会让你头疼得多......特别是当有人输入无效的数字或日期时,因为当你试图使用它们时,修复一下就太晚了输入问题。


必须声明列的数据类型,以便SUM函数能够理解数据。将以下行添加到dataTable定义:

 DataColumn dtc = dt.Columns [  PU]; 
dtc.DataType = System.Type.GetType( System.Double);


i have difficult when i make sum of a column in datagrid also in datatable
i have 4 column in datatable when i make a sum in column Qte it goes normal (integer type)
for column "PU"(double type) it goes with error

please help

What I have tried:

Dim sum As Double

       sum = dt.Compute("SUM(PU)", "")

       MsgBox(sum)

解决方案

Start by going back to your data source and looking at the raw data you have stored: teh error message clearly says "You can't use an aggregation function on a string".
This means that whatever data PU holds, it's stored as a string, not as a number - and you need to convert it to a number in order to SUM the values.

I'd strongly recommend that you start with the original data, and if that is a database change it: always store values in the appropriate data type columns: strings are only for string based data such as names, addresses, mobile phones, email, ... Dates should be in DateTime, numbers should be in int, float, double, or decimal.
All strings may seem like the easy route, but it causes you a lot more headaches later ... particularly when someone enters invalid numbers or dates because when you try to use them it's too late to fix a input problem.


You must declare the column's datatype so that the SUM function understands the data. Add the following line to your dataTable definition:

DataColumn dtc = dt.Columns["PU"];
dtc.DataType = System.Type.GetType("System.Double");


这篇关于当我在datagrid中对datagrid中的列进行求和时,我很难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆