How can I convert a value given by a DataRow cell to double?
Loading
How can I convert a value given by a DataRow cell to double?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sandhiya PriyaPosted Oct 13, 2025, 6:55 AM
To convert a value from a
DataRowcell to adoublein C#,you have a few safe options depending on whether the value can be
nullor might not be convertible. Here’s a detailed guide:1. Using
Convert.ToDoublePros: Handles
nullandDBNull.Valueby throwing an exception.Cons: Will throw if the value is
nullor not numeric.Example with null check:
2. Using
double.TryParseThis is safer if the data might be a string or may fail conversion.
Pros: Won’t throw exceptions.
Cons: Needs extra handling if
null.3. Using casting (if you’re sure the type is numeric)
Only works if the underlying type is already
double. Will throwInvalidCastExceptionif it isn’t.Best Practice
If you’re dealing with a database column that might contain
NULLor string numeric values:Tuhin PaulPosted Sep 26, 2025, 4:05 PM
You can convert a DataRow cell value to double using several methods. Using
Convert.ToDouble()This method handles
nullvalues (converts to0.0) and works with various numeric types.You can also use
double.TryParse()Choose the method that best fits your data certainty and error handling requirements!
Jayraj ChhayaPosted Sep 26, 2025, 11:52 AM
(or
row.Field("ColumnName") if the column is already numeric).