Cyber-Drugs
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!
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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 ?
ASKER
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).
As you have just discovered, DataTables can handle multiple data types which will affect the sorting order (especially for numbers stored as string).
ASKER
Is there an easy way to convert an entire column to a specified data type?
Eg;
dtData.Column[1].DataType = Double;
Eg;
dtData.Column[1].DataType = Double;
Are you populating your DataTable manually or from a database ?
ASKER
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].ToS tring()) };
But that just throws errors.
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].ToS
But that just throws errors.
SOLUTION
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?
ASKER
Unspecified, possible defaulting to string?
I used:
dtData.Columns.Add(new DataColumn("columnName"));
I used:
dtData.Columns.Add(new DataColumn("columnName"));
ASKER
Ok, just tried this:
object[] row = new object[] { dr[0].ToString(), Convert.ToDouble(dr[1].ToS tring()) };
Compiles and runs, but the result is once again, the same as before. Have I missed something?
object[] row = new object[] { dr[0].ToString(), Convert.ToDouble(dr[1].ToS
Compiles and runs, but the result is once again, the same as before. Have I missed something?
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Now THAT works, brilliant, thanks guys!!
ASKER
-100.15
is a lower number than
-171.37
Just to confirm, for that output I used:
MessageBox.Show(dtData.Com
Am I maybe using the incorrect math function?