Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i m still new in vb.net, i have a small problem here during conversion from string to type integer.

i trying loop inside the datagridview, if the system found the data with string "Record Not Found" its will display in yellow color inside the datagridview.
but, when i run my project, error will popup with "conversion from string "Record Not Found" to type integer is not valid.
this is my code,

Dim str As String = "Record Not Found"
For j As Integer = 0 To DGatt.Rows.Count - 1
If DGatt.Rows(j).Cells(0).Value = CType(str, Integer) Then
DGatt.Rows(j).Cells(0).Style.ForeColor = Color.Yellow
End If
Next

how can i settle this problem, i've try using others function but its didnt work at all.
thanks for helping me,
Posted

1 solution

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
 
Share this answer
 
v2
Comments
JOE Heart Under Blade 21-Apr-13 21:45pm    
tq very much for give an solution... i never use this function before... but it's really work... tq very much...
Sergey Alexandrovich Kryukov 21-Apr-13 22:32pm    
Do you mean Parse/TryParse? Sure. You see, you should go away from VB methods left in VB.NET for compatibility, use .NET methods.
Good luck, call again.
—SA
JOE Heart Under Blade 21-Apr-13 23:40pm    
i just do like this. and it's working...

Sub DGatt_CellFormatting(ByVal sender As Object, _
ByVal e As DataGridViewCellFormattingEventArgs) _
Handles DGatt.CellFormatting
If e.Value IsNot Nothing Then
With Me.DGatt.Rows(e.RowIndex).Cells(e.ColumnIndex)
If e.Value.Equals("Record Not Found") Then
.Style.BackColor = Color.Yellow
.ToolTipText = "No record founded in this cell"
End If
End With
End If
End Sub

thanks very much...
Sergey Alexandrovich Kryukov 22-Apr-13 0:39am    
You are very welcome.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900