473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Very odd "If value >= x" Error

I made a page with a gridview that has rows show a different color if a
number in a column is greater than or equal to 45. I also did this
conditional formatting for the column next to it. Here's my code.

in the aspx file
===============================
<asp:GridView ID="gvData" runat="server" OnRowDataBound="doColor">
===============================

in the aspx.vb file
===============================
Sub doColor(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then

If e.Row.Cells(10).Text >= "60" Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(10).ForeColor = Drawing.Color.Red
e.Row.Cells(10).Font.Bold = True
End If
If e.Row.Cells(9).Text >= "45" Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If

End If
End Sub
===============================

The Cells(10) statement works great. Only those results that are greater
than or equal to 60 are in bold red with yellow highlight.

On the Cells(9), however, its another story. For some reason, every
result that is 5 is also bold red highlighted. I tried changing the code
to this:

If e.Row.Cells(9).Text <= "4" Then

And the 5s are no longer in bold red. In fact, if I try any of these

If e.Row.Cells(9).Text >= "9" Then
If e.Row.Cells(9).Text >= "8" Then
If e.Row.Cells(9).Text >= "7" Then
If e.Row.Cells(9).Text >= "6" Then

Then if the text in the cell is 5 it is not in bold red. Once I put in
two digits, like so ..

If e.Row.Cells(9).Text >= "10"

Then the number 5 turns bold red. It should only be bold red if it is 10
or greater!

I also tried this, which works fine; the number 5 (or any other number)
is not in bold red, only the number 55 ...

If e.Row.Cells(9).Text >= "55"

I know i'm trying to do numerical conditions on text, which seem to work
anyway for cell 10. Just to test it, I tried to convert the value of the
text to an integer before the test, like so,

If CInt(e.Row.Cells(9).Text) >= "10"

This only returned an error and I couldn't find any other way to convert
the text into an integer to do the condition check.

Any ideas on this strange error? Its very frustrating.

TIA,
Jim
Aug 17 '06 #1
13 1524
Hi Jim,

You're not going to get the results that you desire by performing textual comparisons as if they are numeric.

You specified that you tried to convert the number but that it is failing, probably due to invalid or null data. To avoid the
conversion error first check if the Text property can be converted to an integer. If you're using the 2.0 framework you can use
Int32.TryParse, otherwise you can just catch the exception.

--
Dave Sexton

"Jim in Arizona" <ti*******@hotmail.comwrote in message news:uG**************@TK2MSFTNGP06.phx.gbl...
>I made a page with a gridview that has rows show a different color if a number in a column is greater than or equal to 45. I also
did this conditional formatting for the column next to it. Here's my code.

in the aspx file
===============================
<asp:GridView ID="gvData" runat="server" OnRowDataBound="doColor">
===============================

in the aspx.vb file
===============================
Sub doColor(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then

If e.Row.Cells(10).Text >= "60" Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(10).ForeColor = Drawing.Color.Red
e.Row.Cells(10).Font.Bold = True
End If
If e.Row.Cells(9).Text >= "45" Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If

End If
End Sub
===============================

The Cells(10) statement works great. Only those results that are greater than or equal to 60 are in bold red with yellow
highlight.

On the Cells(9), however, its another story. For some reason, every result that is 5 is also bold red highlighted. I tried
changing the code to this:

If e.Row.Cells(9).Text <= "4" Then

And the 5s are no longer in bold red. In fact, if I try any of these

If e.Row.Cells(9).Text >= "9" Then
If e.Row.Cells(9).Text >= "8" Then
If e.Row.Cells(9).Text >= "7" Then
If e.Row.Cells(9).Text >= "6" Then

Then if the text in the cell is 5 it is not in bold red. Once I put in two digits, like so ..

If e.Row.Cells(9).Text >= "10"

Then the number 5 turns bold red. It should only be bold red if it is 10 or greater!

I also tried this, which works fine; the number 5 (or any other number) is not in bold red, only the number 55 ...

If e.Row.Cells(9).Text >= "55"

I know i'm trying to do numerical conditions on text, which seem to work anyway for cell 10. Just to test it, I tried to convert
the value of the text to an integer before the test, like so,

If CInt(e.Row.Cells(9).Text) >= "10"

This only returned an error and I couldn't find any other way to convert the text into an integer to do the condition check.

Any ideas on this strange error? Its very frustrating.

TIA,
Jim

Aug 18 '06 #2
Dave Sexton wrote:
Hi Jim,

You're not going to get the results that you desire by performing textual comparisons as if they are numeric.

You specified that you tried to convert the number but that it is failing, probably due to invalid or null data. To avoid the
conversion error first check if the Text property can be converted to an integer. If you're using the 2.0 framework you can use
Int32.TryParse, otherwise you can just catch the exception.
I did try:

If Int32.TryParse(e.Row.Cells(9).Text) >= "45" Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If

But when I do a blue line shows up under the line with the error:
Overload resolution failed because no accessible 'TryParse" accepts this
number of arguments.

Of course, I tried this with no Visual Studio IDE errors shown:

If CInt(e.Row.Cells(9).Text) >= 45 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If

But I get this error when I run the page:
==========================================

One of the identified items was in an invalid format.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.FormatException: One of the identified items
was in an invalid format.

Source Error:
Line 8: If e.Row.RowType = DataControlRowType.DataRow Then
Line 9:
Line 10: If CInt(e.Row.Cells(10).Text) >= 60 Then
Line 11: e.Row.BackColor = Drawing.Color.LightYellow
Line 12:
==========================================

I am using the 2.0 framework and VS2005 Standard.

I don't know what to do at this point. Anyone, Anyone?
Aug 18 '06 #3
Hi Jim,

The first error (blue line) clearly states that you haven't specified all of the required arguments for the TryParse method.

Int32.TryParse on MSDN:
http://msdn2.microsoft.com/en-us/library/f02979c7.aspx

The second error, FormatException, occurs because the Text property of the cell is not returning a value that can be converted into
an integer. As I said in my previous post, you can catch the FormatException if you want, but I think TryParse is the better
choice.

--
Dave Sexton

"Jim in Arizona" <ti*******@hotmail.comwrote in message news:%2****************@TK2MSFTNGP05.phx.gbl...
Dave Sexton wrote:
>Hi Jim,

You're not going to get the results that you desire by performing textual comparisons as if they are numeric.

You specified that you tried to convert the number but that it is failing, probably due to invalid or null data. To avoid the
conversion error first check if the Text property can be converted to an integer. If you're using the 2.0 framework you can use
Int32.TryParse, otherwise you can just catch the exception.

I did try:

If Int32.TryParse(e.Row.Cells(9).Text) >= "45" Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If

But when I do a blue line shows up under the line with the error:
Overload resolution failed because no accessible 'TryParse" accepts this number of arguments.

Of course, I tried this with no Visual Studio IDE errors shown:

If CInt(e.Row.Cells(9).Text) >= 45 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If

But I get this error when I run the page:
==========================================

One of the identified items was in an invalid format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for
more information about the error and where it originated in the code.

Exception Details: System.FormatException: One of the identified items was in an invalid format.

Source Error:
Line 8: If e.Row.RowType = DataControlRowType.DataRow Then
Line 9:
Line 10: If CInt(e.Row.Cells(10).Text) >= 60 Then
Line 11: e.Row.BackColor = Drawing.Color.LightYellow
Line 12:
==========================================

I am using the 2.0 framework and VS2005 Standard.

I don't know what to do at this point. Anyone, Anyone?

Aug 18 '06 #4
Dave Sexton wrote:
Hi Jim,

The first error (blue line) clearly states that you haven't specified all of the required arguments for the TryParse method.

Int32.TryParse on MSDN:
http://msdn2.microsoft.com/en-us/library/f02979c7.aspx

The second error, FormatException, occurs because the Text property of the cell is not returning a value that can be converted into
an integer. As I said in my previous post, you can catch the FormatException if you want, but I think TryParse is the better
choice.
I gave this a try:
========================================
Sub doColor(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then

If Int32.TryParse(e.Row.Cells(10).Text,
Globalization.NumberStyles.Any) >= 60 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(10).ForeColor = Drawing.Color.Red
e.Row.Cells(10).Font.Bold = True
End If
If Int32.TryParse(e.Row.Cells(9).Text,
Globalization.NumberStyles.Any) >= 45 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If

End If
End Sub
========================================
Although the page loaded, no rows were highlighted or values in bold
red, even though there were matching values that should have been (for
Cells(10) OR Cells(9)).

I tried this (not the try/catch and Int32.Parse):
========================================
Sub doColor(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Try
If Int32.Parse(e.Row.Cells(10).Text,
Globalization.NumberStyles.Any) >= 60 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(10).ForeColor = Drawing.Color.Red
e.Row.Cells(10).Font.Bold = True
End If
If Int32.Parse(e.Row.Cells(9).Text,
Globalization.NumberStyles.Any) >= 45 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If
Catch
End Try

End If
End Sub
========================================

But when I did this, the Cells(9) didn't work. One row that had a value
greater than 45 (the value was 55) did not change color like it should have.

Cells(10) worked as intended (as it always seems to do).

So, how could I disregard NULL values in the returned data, since this
is probably the cause of all my woes?
Aug 18 '06 #5
Hi Jim,

Your first attempt shouldn't have even built (I suspect that it did because VB doesn't require explicitly marking "out" arguments
and that's a shame). You still don't have the arguments correct for the Int32.TryParse method. Look at the link I posted for more
info.

In your second attempt you are not catching FormatException, you are catching all Exceptions. I must advise against that.

I've already answered your last question with two possible solutions. Maybe you are miscounting the row index, which is zero-based.
i.e. The first column has an index of zero, the second column has an index of one, the third column has an index of two, etc. You
also have to account for hidden columns when determining the index to be used.

--
Dave Sexton

"Jim in Arizona" <ti*******@hotmail.comwrote in message news:ef**************@TK2MSFTNGP03.phx.gbl...
Dave Sexton wrote:
>Hi Jim,

The first error (blue line) clearly states that you haven't specified all of the required arguments for the TryParse method.

Int32.TryParse on MSDN:
http://msdn2.microsoft.com/en-us/library/f02979c7.aspx

The second error, FormatException, occurs because the Text property of the cell is not returning a value that can be converted
into an integer. As I said in my previous post, you can catch the FormatException if you want, but I think TryParse is the
better choice.

I gave this a try:
========================================
Sub doColor(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then

If Int32.TryParse(e.Row.Cells(10).Text, Globalization.NumberStyles.Any) >= 60 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(10).ForeColor = Drawing.Color.Red
e.Row.Cells(10).Font.Bold = True
End If
If Int32.TryParse(e.Row.Cells(9).Text, Globalization.NumberStyles.Any) >= 45 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If

End If
End Sub
========================================
Although the page loaded, no rows were highlighted or values in bold red, even though there were matching values that should have
been (for Cells(10) OR Cells(9)).

I tried this (not the try/catch and Int32.Parse):
========================================
Sub doColor(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Try
If Int32.Parse(e.Row.Cells(10).Text, Globalization.NumberStyles.Any) >= 60 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(10).ForeColor = Drawing.Color.Red
e.Row.Cells(10).Font.Bold = True
End If
If Int32.Parse(e.Row.Cells(9).Text, Globalization.NumberStyles.Any) >= 45 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If
Catch
End Try

End If
End Sub
========================================

But when I did this, the Cells(9) didn't work. One row that had a value greater than 45 (the value was 55) did not change color
like it should have.

Cells(10) worked as intended (as it always seems to do).

So, how could I disregard NULL values in the returned data, since this is probably the cause of all my woes?

Aug 18 '06 #6
Int.TryParse returns a bool so you want something like (in C#):

int Globalization.NumberStyles.Any;
int.TryParse(e.Row.Cells(10).Text, Globalization.NumberStyles.Any);

if (Globalization.NumberStyles.Any 45)
{
}
etc...

Jim in Arizona wrote:
Dave Sexton wrote:
>Hi Jim,

The first error (blue line) clearly states that you haven't specified
all of the required arguments for the TryParse method.

Int32.TryParse on MSDN:
http://msdn2.microsoft.com/en-us/library/f02979c7.aspx

The second error, FormatException, occurs because the Text property of
the cell is not returning a value that can be converted into an
integer. As I said in my previous post, you can catch the
FormatException if you want, but I think TryParse is the better choice.

I gave this a try:
========================================
Sub doColor(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then

If Int32.TryParse(e.Row.Cells(10).Text,
Globalization.NumberStyles.Any) >= 60 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(10).ForeColor = Drawing.Color.Red
e.Row.Cells(10).Font.Bold = True
End If
If Int32.TryParse(e.Row.Cells(9).Text,
Globalization.NumberStyles.Any) >= 45 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If

End If
End Sub
========================================
Although the page loaded, no rows were highlighted or values in bold
red, even though there were matching values that should have been (for
Cells(10) OR Cells(9)).

I tried this (not the try/catch and Int32.Parse):
========================================
Sub doColor(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Try
If Int32.Parse(e.Row.Cells(10).Text,
Globalization.NumberStyles.Any) >= 60 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(10).ForeColor = Drawing.Color.Red
e.Row.Cells(10).Font.Bold = True
End If
If Int32.Parse(e.Row.Cells(9).Text,
Globalization.NumberStyles.Any) >= 45 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If
Catch
End Try

End If
End Sub
========================================

But when I did this, the Cells(9) didn't work. One row that had a value
greater than 45 (the value was 55) did not change color like it should
have.

Cells(10) worked as intended (as it always seems to do).

So, how could I disregard NULL values in the returned data, since this
is probably the cause of all my woes?
Aug 18 '06 #7
Ooops, missed the out keyword

int Globalization.NumberStyles.Any;
int.TryParse(e.Row.Cells(10).Text, out Globalization.NumberStyles.Any);

if (Globalization.NumberStyles.Any 45)
{
}
Kevin Jones wrote:
Int.TryParse returns a bool so you want something like (in C#):

int Globalization.NumberStyles.Any;
int.TryParse(e.Row.Cells(10).Text, Globalization.NumberStyles.Any);

if (Globalization.NumberStyles.Any 45)
{
}
etc...

Jim in Arizona wrote:
>Dave Sexton wrote:
>>Hi Jim,

The first error (blue line) clearly states that you haven't specified
all of the required arguments for the TryParse method.

Int32.TryParse on MSDN:
http://msdn2.microsoft.com/en-us/library/f02979c7.aspx

The second error, FormatException, occurs because the Text property
of the cell is not returning a value that can be converted into an
integer. As I said in my previous post, you can catch the
FormatException if you want, but I think TryParse is the better choice.

I gave this a try:
========================================
Sub doColor(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then

If Int32.TryParse(e.Row.Cells(10).Text,
Globalization.NumberStyles.Any) >= 60 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(10).ForeColor = Drawing.Color.Red
e.Row.Cells(10).Font.Bold = True
End If
If Int32.TryParse(e.Row.Cells(9).Text,
Globalization.NumberStyles.Any) >= 45 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If

End If
End Sub
========================================
Although the page loaded, no rows were highlighted or values in bold
red, even though there were matching values that should have been (for
Cells(10) OR Cells(9)).

I tried this (not the try/catch and Int32.Parse):
========================================
Sub doColor(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Try
If Int32.Parse(e.Row.Cells(10).Text,
Globalization.NumberStyles.Any) >= 60 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(10).ForeColor = Drawing.Color.Red
e.Row.Cells(10).Font.Bold = True
End If
If Int32.Parse(e.Row.Cells(9).Text,
Globalization.NumberStyles.Any) >= 45 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If
Catch
End Try

End If
End Sub
========================================

But when I did this, the Cells(9) didn't work. One row that had a
value greater than 45 (the value was 55) did not change color like it
should have.

Cells(10) worked as intended (as it always seems to do).

So, how could I disregard NULL values in the returned data, since this
is probably the cause of all my woes?
Aug 18 '06 #8
Hi Kevin,

Your example wouldn't even compile.

System.Globalization.NumberStyles is an enumeration, not a variable name. Also, the method returns a Boolean, as you mentioned, but
in your example your not event using it.

Int32.TryParse method on MSDN:
http://msdn2.microsoft.com/en-us/library/f02979c7.aspx

I recommend taking a look.

--
Dave Sexton

"Kevin Jones" <kj****@develop.comwrote in message news:%2****************@TK2MSFTNGP02.phx.gbl...
Ooops, missed the out keyword

int Globalization.NumberStyles.Any;
int.TryParse(e.Row.Cells(10).Text, out Globalization.NumberStyles.Any);

if (Globalization.NumberStyles.Any 45)
{
}
Kevin Jones wrote:
>Int.TryParse returns a bool so you want something like (in C#):

int Globalization.NumberStyles.Any;
int.TryParse(e.Row.Cells(10).Text, Globalization.NumberStyles.Any);

if (Globalization.NumberStyles.Any 45)
{
}
etc...

Jim in Arizona wrote:
>>Dave Sexton wrote:
Hi Jim,

The first error (blue line) clearly states that you haven't specified all of the required arguments for the TryParse method.

Int32.TryParse on MSDN:
http://msdn2.microsoft.com/en-us/library/f02979c7.aspx

The second error, FormatException, occurs because the Text property of the cell is not returning a value that can be converted
into an integer. As I said in my previous post, you can catch the FormatException if you want, but I think TryParse is the
better choice.
I gave this a try:
========================================
Sub doColor(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then

If Int32.TryParse(e.Row.Cells(10).Text, Globalization.NumberStyles.Any) >= 60 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(10).ForeColor = Drawing.Color.Red
e.Row.Cells(10).Font.Bold = True
End If
If Int32.TryParse(e.Row.Cells(9).Text, Globalization.NumberStyles.Any) >= 45 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If

End If
End Sub
========================================
Although the page loaded, no rows were highlighted or values in bold red, even though there were matching values that should
have been (for Cells(10) OR Cells(9)).

I tried this (not the try/catch and Int32.Parse):
========================================
Sub doColor(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Try
If Int32.Parse(e.Row.Cells(10).Text, Globalization.NumberStyles.Any) >= 60 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(10).ForeColor = Drawing.Color.Red
e.Row.Cells(10).Font.Bold = True
End If
If Int32.Parse(e.Row.Cells(9).Text, Globalization.NumberStyles.Any) >= 45 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If
Catch
End Try

End If
End Sub
========================================

But when I did this, the Cells(9) didn't work. One row that had a value greater than 45 (the value was 55) did not change color
like it should have.

Cells(10) worked as intended (as it always seems to do).

So, how could I disregard NULL values in the returned data, since this is probably the cause of all my woes?

Aug 18 '06 #9
Dave Sexton wrote:
Your example wouldn't even compile.
I'm typing the code into notepad - I was just trying to get across the
idea that you can't use the return from TryParse as that is a bool, but
instead need to set the "out" variable to get the actual result of
TryParse.
System.Globalization.NumberStyles is an enumeration, not a variable
name.
I didn't bother checking the type of the variable he was using, assuming
it would be an int
Also, the method returns a Boolean, as you mentioned, but
in your example your not event using it.
And in this case I don't need to. if the method fails then the value
being set will still be 0 which is less than 45 the last time I looked!
I recommend taking a look.
Thanks for the advice but I now exactly how it works.
Hi Kevin,
System.Globalization.NumberStyles is an enumeration, not a variable name. Also, the method returns a Boolean, as you mentioned, but
in your example your not event using it.

Int32.TryParse method on MSDN:
http://msdn2.microsoft.com/en-us/library/f02979c7.aspx

Aug 18 '06 #10
Dave Sexton wrote:
Hi Jim,

Your first attempt shouldn't have even built (I suspect that it did because VB doesn't require explicitly marking "out" arguments
and that's a shame). You still don't have the arguments correct for the Int32.TryParse method. Look at the link I posted for more
info.

In your second attempt you are not catching FormatException, you are catching all Exceptions. I must advise against that.

I've already answered your last question with two possible solutions. Maybe you are miscounting the row index, which is zero-based.
i.e. The first column has an index of zero, the second column has an index of one, the third column has an index of two, etc. You
also have to account for hidden columns when determining the index to be used.
Hi Dave. Thanks for your continued help.

Ok, after looking at the examples on MSDN (that link you provided on
int32.parse), here's what I confusingly built. It works, as far as I can
tell, but what do you think? Is it right or close to right? This code is
much more complex that I'm used to conjuring up.

================================================== =======

Sub doColor(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then

Dim s9 As String = e.Row.Cells(9).Text
Dim result9 As Integer
Dim returnValue9 As Boolean
returnValue9 = Integer.TryParse(s9, result9)

Dim s10 As String = e.Row.Cells(10).Text
Dim result10 As Integer
Dim returnValue10 As Boolean
returnValue10 = Integer.TryParse(s10, result10)

If Int32.TryParse(e.Row.Cells(9).Text, result9) Then
Try
If returnValue9 = True Then
If Int32.Parse(e.Row.Cells(9).Text) >= 45 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If
End If
Catch ex As System.FormatException

End Try
End If

If Int32.TryParse(e.Row.Cells(10).Text, result10) Then
Try
If returnValue10 = True Then
If Int32.Parse(e.Row.Cells(10).Text) >= 60 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(10).ForeColor = Drawing.Color.Red
e.Row.Cells(10).Font.Bold = True
End If
End If
Catch ex As System.FormatException

End Try
End If

End If
End Sub

================================================== =======
Aug 18 '06 #11
Kevin Jones wrote:
Ooops, missed the out keyword

int Globalization.NumberStyles.Any;
int.TryParse(e.Row.Cells(10).Text, out Globalization.NumberStyles.Any);

if (Globalization.NumberStyles.Any 45)
{
}
Kevin Jones wrote:
>Int.TryParse returns a bool so you want something like (in C#):

int Globalization.NumberStyles.Any;
int.TryParse(e.Row.Cells(10).Text, Globalization.NumberStyles.Any);

if (Globalization.NumberStyles.Any 45)
{
}
etc...
Thanks for the Help, Kevin. Although I don't know anything about C#,
your examples still helped give me a rough clue as to how I should start
thinking.
Aug 18 '06 #12
Hi Jim,

I guess that would work but you definately added more than is required.

1. Choose one of either Try...Catch FormatException or Int32.TryParse, but not both.
2. Int32.TryParse will replace any need for Int32.Parse, so you don't need both.

'This example uses Int32.TryParse to convert cell text to an integer
Dim i9 As Integer ' this variable will hold the value of cell 9

If Int32.TryParse(e.Row.Cells(9).Text, i9) _
AndAlso i9 >= 45 Then
' TryParse returns a value that indicates whether the conversion was successful
' so we only want to enter this block if TryParse returns True.
' i9 is set to the converted value if TryParse succeeds
' so immediately after TryParse we check AndAlso i9 >= 45

e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If

You might have to fix up my VB, so beware!

--
Dave Sexton

"Jim in Arizona" <ti*******@hotmail.comwrote in message news:%2****************@TK2MSFTNGP03.phx.gbl...
Dave Sexton wrote:
>Hi Jim,

Your first attempt shouldn't have even built (I suspect that it did because VB doesn't require explicitly marking "out" arguments
and that's a shame). You still don't have the arguments correct for the Int32.TryParse method. Look at the link I posted for
more info.

In your second attempt you are not catching FormatException, you are catching all Exceptions. I must advise against that.

I've already answered your last question with two possible solutions. Maybe you are miscounting the row index, which is
zero-based. i.e. The first column has an index of zero, the second column has an index of one, the third column has an index of
two, etc. You also have to account for hidden columns when determining the index to be used.

Hi Dave. Thanks for your continued help.

Ok, after looking at the examples on MSDN (that link you provided on int32.parse), here's what I confusingly built. It works, as
far as I can tell, but what do you think? Is it right or close to right? This code is much more complex that I'm used to conjuring
up.

================================================== =======

Sub doColor(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then

Dim s9 As String = e.Row.Cells(9).Text
Dim result9 As Integer
Dim returnValue9 As Boolean
returnValue9 = Integer.TryParse(s9, result9)

Dim s10 As String = e.Row.Cells(10).Text
Dim result10 As Integer
Dim returnValue10 As Boolean
returnValue10 = Integer.TryParse(s10, result10)

If Int32.TryParse(e.Row.Cells(9).Text, result9) Then
Try
If returnValue9 = True Then
If Int32.Parse(e.Row.Cells(9).Text) >= 45 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If
End If
Catch ex As System.FormatException

End Try
End If

If Int32.TryParse(e.Row.Cells(10).Text, result10) Then
Try
If returnValue10 = True Then
If Int32.Parse(e.Row.Cells(10).Text) >= 60 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(10).ForeColor = Drawing.Color.Red
e.Row.Cells(10).Font.Bold = True
End If
End If
Catch ex As System.FormatException

End Try
End If

End If
End Sub

================================================== =======

Aug 18 '06 #13
Dave Sexton wrote:
Hi Jim,

I guess that would work but you definately added more than is required.

1. Choose one of either Try...Catch FormatException or Int32.TryParse, but not both.
2. Int32.TryParse will replace any need for Int32.Parse, so you don't need both.

'This example uses Int32.TryParse to convert cell text to an integer
Dim i9 As Integer ' this variable will hold the value of cell 9

If Int32.TryParse(e.Row.Cells(9).Text, i9) _
AndAlso i9 >= 45 Then
' TryParse returns a value that indicates whether the conversion was successful
' so we only want to enter this block if TryParse returns True.
' i9 is set to the converted value if TryParse succeeds
' so immediately after TryParse we check AndAlso i9 >= 45

e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If

You might have to fix up my VB, so beware!
It amazes me how I always end up turning something simple into something
overly complex. I didn't understand that i9 would hold the actual value
of the cell. I thought it was only going to hold a boolean of weather
the cell could be successfully converted to Int32. Thinking that way, I
wrote much more code to produce the same result. Here's what I now have
with your help. Thanks.
================================================== ===========
If e.Row.RowType = DataControlRowType.DataRow Then
Dim i9, i10 As Integer

If Int32.TryParse(e.Row.Cells(9).Text, i9) AndAlso i9 >= 45 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If

If Int32.TryParse(e.Row.Cells(9).Text, i9) AndAlso i9 < 0 Then
e.Row.BackColor = Drawing.Color.Gold
e.Row.Cells(9).ForeColor = Drawing.Color.Red
e.Row.Cells(9).Font.Bold = True
End If

If Int32.TryParse(e.Row.Cells(10).Text, i10) AndAlso i10 >= 60 Then
e.Row.BackColor = Drawing.Color.LightYellow
e.Row.Cells(10).ForeColor = Drawing.Color.Red
e.Row.Cells(10).Font.Bold = True
End If
End If
================================================== ===========
Aug 18 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lvcha.gouqizi | last post by:
I embed an applet in my php script which has a parameter providing an input file for the jar. Does this file has size limit? The code is as follows: <APPLET ARCHIVE="myapplet.jar" WIDTH=372...
1
by: Christian Schmidbauer | last post by:
Hello! I prepare my XML document like this way: ------------------------------------------------------- PrintWriter writer; Document domDocument; Element domElement; // Root tag
7
by: Diandian Zhang | last post by:
Does anyone have an idea, how to do it? Thanks in advance!
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
16
by: Tantale | last post by:
I used this serviec to check my webpage http://www.jmrw.com/Abroad/Barcelone/index.htm Made with Dreamweaver 8. The result is 206 errors, most of them "end tag omitted, but OMITTAG NO was...
15
by: arnuld | last post by:
-------- PROGRAMME ----------- /* Stroustrup, 5.6 Structures STATEMENT: this programmes *tries* to do do this in 3 parts: 1.) it creates a "struct", named "jd", of type "address". 2. it...
11
by: jjw92 | last post by:
I've been banging my head against the wall with this one for a couple days so I'm hoping someone has some ideas. I have a web service that I created that is called by a .NET class library (which in...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
1
by: ChollaPete | last post by:
This code: <form action="processScan.php" method="get"> <p> <?php print "Scan name: <input type=\"file\" name=\"tScanFileName\" value= \"{$scanFileName}\"><br>"; addHiddenCarryons(); ?>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.