简体   繁体   English

VB.Net-如何将GridView中的2列比较为数字?

[英]VB.Net - How to compare 2 columns in GridView as numbers?

I have a GridView which I plan to add different colors in each row based on certain conditions. 我有一个GridView,我计划根据某些条件在每行中添加不同的颜色。

One such condition is to color the row in a specific color if a number in column 6 is higher than the number in column 7. 一种这样的条件是,如果第6列中的数字大于第7列中的数字,则以特定颜色为行着色。

 If e.Row.Cells(6).Text > e.Row.Cells(7).Text Then
            e.Row.BackColor = ColorTranslator.FromHtml("#FDD533")
 Else

However, when I test the code it seems to only change the color when the left most value in column 6 is larger than column 7, regardless of the fact that column 6 is in the hundreds, while column 7 is in the thousands. 但是,当我测试代码时,似乎只有在第6列的最左值大于第7列时才更改颜色,而不管第6列有几百个而第7列有几千个。

I believe the cause is coming from me using text in the If statement. 我相信原因来自我, If使用If语句中的text However, any instance of number I can think to change doesn't seem to fit into the code: 但是,我认为可以更改的任何数字实例似乎都不适合代码:

If e.Row.Cells(6).Number > e.Row.Cells(7).Number Then

or 要么

If e.Row.Cells(6).Integer > e.Row.Cells(7).Integer Then

These example gives me a message saying 这些例子给我一个信息说

'Number' is not a member of 'System.Web.UI.WebControls.TableCell'

What else can I use that is compatible with the current code? 我还能使用什么与当前代码兼容的东西?

I believe that you are missing a casting. 我相信您缺少演员表。 I'm not sure if you're comparing only Integers or complex numbers , if you don't know I recommend you to cast directly to doubles : 我不确定您是只比较整数还是复杂数字 ,如果您不知道我建议您直接将其转换为双精度数

If Convert.ToDouble(e.Row.Cells(6).Text) > Convert.ToDouble(e.Row.Cells(7).Text) Then
      e.Row.BackColor = ColorTranslator.FromHtml("#FDD533")
Else

Convert.ToDouble will convert any text to number. Convert.ToDouble会将任何文本转换为数字。

More info about casting: 有关投放的更多信息:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number

https://www.dotnetheaven.com/article/casting-integer-to-long-single-and-double-using-vb.net https://www.dotnetheaven.com/article/casting-integer-to-long-single-and-double-using-vb.net

If you might have some empty values you can use the TryCast : 如果您可能有一些空值 ,可以使用TryCast

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/trycast-operator https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/trycast-operator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM