I am inserting a grid, with a debbug I realize that if I am receiving the data, some are decimal, as you can see I do the conversion, but it turns out that there may be an 'X' in these, so it throws an exception,
So I need that if its value is an X, I change it to 0 (zero) and that in this way I can do the insert correctly.
How can I achieve it?
foreach (DataGridViewRow item in datagrid.Rows)
{
comando.Parameters.Add(":Stock", item.Cells[2].Value.ToString());
comando.Parameters.Add(":Cant", float.Parse(item.Cells[3].Value.ToString()));
comando.Parameters.Add(":Units", float.Parse(item.Cells[4].Value.ToString()));
.
.
.
}
Make yourself a helper functor in your work context:
The above functor returns
0
if the value could not be converted to a number.You can use the
TryParse()
in this way you define a default value and if theX
does not let you convert correctly nothing will happenSomething like this
If the problem is the culture that you have applied, you could use
This way you make sure that when receiving a numerical value with the point as decimal separator, it recognizes it