Click here to Skip to main content
14,303,095 members
Rate this:
Please Sign up or sign in to vote.
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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
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.'}
OriginalGriff 25-Apr-19 10:05am
   
Use the debugger and find out why: 8.24 is a valid Double value, so the actual string you are converting must include characters that aren't numeric or decimal point, or the Culture settings of the PC are set to use a characters other than '.' as a decimal separator.
Member 14207792 25-Apr-19 12:10pm
   
yes this is the problem thank god now i can figure it out
i change the "." with "," and it work and goes to the next value
now i need to set that "," to be "."
any idea how i can do it please
Rate this:
Please Sign up or sign in to vote.

Solution 2

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");
   
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;
Member 14207792 26-Apr-19 11:13am
   
i still haven't resolve it always it demand to be "," not "."
OriginalGriff 25-Apr-19 12:42pm
   
Best way is not to use the casting operations, but use TryParse instead - which lets you specify the Culture that the number will be expected to be in:
https://docs.microsoft.com/en-us/dotnet/api/system.double.tryparse?view=netframework-4.8#System_Double_TryParse_System_String_System_Globalization_NumberStyles_System_IFormatProvider_System_Double__
Richard MacCutchan 25-Apr-19 7:14am
   
You are adding the column data as text strings. You must convert it to double values first.
Member 14207792 25-Apr-19 9:40am
   
i tested everything and no soltuion
i tried to delete the number after the point and it work
so what i can ndo in this case
Richard MacCutchan 25-Apr-19 10:05am
   
See the suggestion from Richard Deeming above.
Member 14207792 25-Apr-19 7:22am
   
only string that work with column "PU"
i tried Int32 and double always same error
{'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 8:38am
   
You need to learn the difference between strings and numbers.
Member 14207792 25-Apr-19 9:22am
   
maybe
Richard MacCutchan 25-Apr-19 10:03am
   
I don't think there is any maybe about it. If you do not understand how to import a textual representation of a number and convert it to a proper computer based numerical value, then you are going to face many problems.
Member 14207792 25-Apr-19 7:24am
   
its like the point that in textfile made this difficult for me

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100