By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

Very odd "If value >= x" Error

P: n/a
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
Share this Question
Share on Google+
13 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.