I’m saving values entered in a DGV to a List<T>; they are decimal values or nothing (empty). If empty, I want them to be viewed as null, so that my helper function will see those empty vals as 0. But this line here:
rrsr.DuckbillRate = Convert.ToDecimal(GetValAtColRow(colNumber, rowNumber));
…sometimes fails with “Input string not in a correct format” (when it fails, the value it’s trying to convert is a string.empty):
Here are my helper functions to get the cell values, so that I can save them in my List<T>, and restore/repopulate the cells from the contents of those records stored in the List<T>:
private string GetValAtColRow(int colNum, int rowNum)
{
DataGridViewRow desiredRow = dataGridViewPlatypi.Rows[rowNum];
return (desiredRow.Cells[colNum].Value ?? 0).ToString();
}
private void SetValAtColRow(int colNum, int rowNum, string val)
{
string convertedVal = val;
// If the value is 0, I want to display an empty cell (nothing)
if (convertedVal.Equals("0"))
{
convertedVal = string.Empty;
}
DataGridViewRow desiredRow = dataGridViewPlatypi.Rows[rowNum];
desiredRow.Cells[colNum].Value = convertedVal;
}
parsing is becoming wrong somewhere…
Input string not in a correct format error usually comes when parsing is not done correctly…
Regarding other issue…try to do like this>>>