The first thing you need to check is if
dr[1]
is not null (assuming there are at least two columns in the data table).
Then you can use the method
dr[1].ToString()
to convert to a string.
[Updated: Also takes care of empty string]
The "long winded" way for easy reading:
decimal Total_Sqrft = 0;
foreach (DataRow dr in gv1.Rows)
{
Total_Sqrft = 0;
if (dr[1] != null)
{
string tmp = dr[1].ToString();
if (tmp != String.Empty)
Total_Sqrft = decimal.Parse(tmp);
}
}
The one-liner
decimal Total_Sqrft = 0;
foreach (DataRow dr in gv1.Rows)
{
Total_Sqrft = decimal.Parse((dr[1] == null || dr[1].ToString() == String.Empty) ? "0" : dr[1].ToString());
}
You can also look into
decimal.TryParse(...)