Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET strings
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 19-Apr-13 19:46pm
JOE MIS2.2K

1 solution

Rate this: bad
good
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:
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
  Permalink  
v2
Comments
JOE MIS at 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 at 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 MIS at 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 at 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)

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 329
2 CPallini 270
3 Afzaal Ahmad Zeeshan 204
4 DamithSL 194
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 20 Apr 2013
Copyright © CodeProject, 1999-2014
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