What do you want really? To have the string "Record Not Found" "converted" to integer? With the value -1 or, perhaps, 666? :-)
You explicitly assigned this value to a string in first line, why? you never use it.
First of all, if you want to store integer in some cell, store integer, not a string. Use the cell type derived from
System.Windows.Forms.DataGridViewCell
to store
Integer
and not anything else. If you need to edit this value in place, use
System.Windows.Forms.DataGridViewTextBoxCell
:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewtextboxcell.aspx[
^].
In all cases, before type-casting the cell
Value
, check the value type:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewtextboxcell.aspx[
^].
And, finally, if you have a string which you need to parse as integer but nut sure it is always successful (as in the case with
DataGridViewTextBoxCell
, see above), don't use VB-specific
CType
, use civilized and explicitly named
System.Int32.Parse
(will throw exception if parsing is not successful) or
System.Int32.TryParse
; see also the parsing method with the same names for all other numeric types.
—SA