Click here to Skip to main content
11,932,048 members (57,969 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB string VB.NET
i m still new in, 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

how can i settle this problem, i've try using others function but its didnt work at all.
thanks for helping me,
Posted 19-Apr-13 19:46pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

What do you want really? To have the string "Record Not Found" "converted" to integer? With the value -1 or, perhaps, 666? Smile | :)
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:[^],[^].

In all cases, before type-casting the cell Value, check the value type:[^].

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.

JOE MIS 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.
JOE MIS 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...
You are very welcome.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 20 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100