Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
Posted
Updated 24-Apr-19 23:26pm
Comments
Member 14207792 25-Apr-19 4:43am    
the erro when i execute is
"Utilisation non valide de la fonction d'agrégation Sum() et du type : String.'"
 Dim sum As Object

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

        MsgBox(sum.ToString)
Richard MacCutchan 25-Apr-19 5:03am    
The error message suggests that "PU" is not a numeric column.
Member 14207792 25-Apr-19 5:18am    
what should i do

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.
 
Share this answer
 
Comments
Member 14207792 25-Apr-19 6:05am    
thanks for help

i get this column where i have error is from textfile and textfile converted from excel and excel from (pdf facture manually entered)this is the source
now i should have sum of this column("PU")
dt.Columns.Add("PU")
this is how i add the column PU without a parametre gettype
OriginalGriff 25-Apr-19 6:14am    
So when you read your text file, you convert the columns into the appropriate data types instead of just treating them as a string.
Member 14207792 25-Apr-19 9:41am    
yes exactly i converted it in many way to a double and when execute the sum no result
OriginalGriff 25-Apr-19 9:54am    
And when you converted it, what did you do with the converted values?
Member 14207792 25-Apr-19 9:58am    
fisrt step i convert the column type directly to double and gives me an error says that data is can't be double like
Nrow("PU") = CDbl(TxtVals(3))

error is
{ 'La conversion de la chaîne "8.24" en type 'Double' n'est pas valide.'}
You must declare the column's datatype so that the SUM function understands the data. Add the following line to your dataTable definition:
C#
DataColumn dtc = dt.Columns["PU"];
dtc.DataType = System.Type.GetType("System.Double");
 
Share this answer
 
Comments
Member 14207792 25-Apr-19 6:07am    
when i try to add datatype to the column i have error
  dt.Columns.Add("PU", GetType(Double))


{'Le format de la chaîne d'entrée est incorrect.Impossible de stocker <71.84> dans la colonne PU. Type attendu est Double.'}
Richard MacCutchan 25-Apr-19 6:27am    
There is something wrong with your data, but we cannot see it.
Richard Deeming 25-Apr-19 9:45am    
At a guess, is your computer set to a culture which uses "," as the decimal separator, but the data you're trying to import is using "." instead?
Member 14207792 25-Apr-19 9:48am    
the data i import use "."
and how to set the computer to use "," or "."
Richard Deeming 25-Apr-19 9:56am    
For .NET 4.5 or higher:
CultureInfo.DefaultThreadCurrentCulture Property (System.Globalization) | Microsoft Docs[^]

This will set your application to the "invariant" culture, which is effectively US English:
CultureInfo culture = CultureInfo.InvariantCulture;

CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;

Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900