Link to home
Create AccountLog in
Avatar of Cyber-Drugs
Cyber-DrugsFlag for United Kingdom of Great Britain and Northern Ireland

asked on

C# - Determine biggest and smallest number in a DataTables (handling decimal values)

Hi guys 'n gals,

I have a DataTable, and in one column is a long list of numbers (positive and negative with decimal values). I want to know what would be the best method to determine the biggest and smallest number in this column?

Cheers!
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Cyber-Drugs

ASKER

Unfortunately that doesn't quite work, as it believes:

-100.15

is a lower number than

-171.37


Just to confirm, for that output I used:

MessageBox.Show(dtData.Compute("Min(ColumnName)", string.Empty).ToString());


Am I maybe using the incorrect math function?
It sounds more likely that the problem will be with the way you are storing your values. Are you by any chance storing your values as a text-based datatype rather than a numeric type ?
I am, I wasn't aware DataTables could handle multiple data types though, which type would you recommend I change it to? Double? Other?
double should work ok.

As you have just discovered, DataTables can handle multiple data types which will affect the sorting order (especially for numbers stored as string).
Is there an easy way to convert an entire column to a specified data type?

Eg;

dtData.Column[1].DataType = Double;
Are you populating your DataTable manually or from a database ?
A bit long winded, but here we go...

I have a function, which reads text file, and does some string manipulation, and then dumps row by row into a DataTable, and then returns the DataTable to whatever calls the DataTable.

I then do the following to fill the live DataTable:

DataTable dtTemp = SVReader();
foreach (DataRow dr in dtTemp.Rows)
{
    string[] row = new string[] { dr[0].ToString(), dr[1].ToString() };
    dtData.Rows.Add(row);
}



Is it possible to make the change in that last loop? I've tried:

string[] row = new string[] { dr[0].ToString(), Convert.ToDouble(dr[1].ToString()) };

But that just throws errors.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
when you created your column into the datatable object, what datatype have you provided?
Unspecified, possible defaulting to string?

I used:

dtData.Columns.Add(new DataColumn("columnName"));
Ok, just tried this:


object[] row = new object[] { dr[0].ToString(), Convert.ToDouble(dr[1].ToString()) };

Compiles and runs, but the result is once again, the same as before. Have I missed something?
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Now THAT works, brilliant, thanks guys!!