This is what i’ve tried:
Decimal tempLow = 0;
for (int irow = 0; irow < dtRep.Rows.Count; irow++)
{
DataRow row = dtRep.Rows[irow];
if (irow == 0)
tempLow = Convert.ToDecimal(row[col.ToString()]);
if (tempLow > Convert.ToDecimal(row[col.ToString()]))
tempLow = Convert.ToDecimal(row[col.ToString()]);
}
You can use
LINQto get the lowest value without your cumbersome loop.If you’re using .NET 2.0 or lower, you could use DataTable.Compute:
Edit: As mentioned in a comment to my answer, you’re also iterating all DataColumns of the DataTable, hence the column-names are dynamic.
Try this approach which loops all columns, checks if the column’s DataType is “numeric” and detects the lowest value of all rows in all columns:
Note: Exclude the typecheck if you know that all columns are numeric or that they contain strings which can be converted to decimals. (
DataTable.Computewill not throw an error if the DataColumn’sDataTypeis a String, butConvert.ToDecimalwill if the string is not convertible)Finally: Here’s the “bruteforce” way, iterating all values in the
DataTableand trying to parse them to a Decimal: