locked
error in cell RRS feed

  • Question

  • User66371569 posted

    Hi i have cell

    id val
    1 -1280
    2 33
    3 5120
    4 0

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For Each row As GridViewRow In GridView2.Rows
               
                Dim val As Decimal = Decimal.Parse(row.Cells(1).Text())
    
                If val > 0 And val < 60 Then
                    row.Cells(1).BackColor = Drawing.Color.Orange
                End If
    
              
            Next
        End Sub

    Dim val As Decimal = Decimal.Parse(row.Cells(1).Text())

    Input string was not in a correct format.

    How to solve it

    Tuesday, April 27, 2021 7:32 AM

All replies

  • User475983607 posted

    First, it's Text not Text().  Text is a property not a method.   I recommend using the debugger to view the value of cell(1).  You could have the wrong cell or the value is within a server control like a label.

    Keep in mind, it is a lot easier to provide assistance when sharing code the community can actually run.

    Tuesday, April 27, 2021 11:05 AM
  • User-1716253493 posted

    To bypass error

    Dim val As Decimal
    Dim correctvalue As Boolean = Decimal.TryParse(row.Cells(1).Text, val)

    Tuesday, April 27, 2021 2:34 PM
  • User66371569 posted

    when i  make

    if correctvalue < 0 then

    .......background.color.......

    end if

    its not  reading values  < 0   

       >0    and <90  also  not reading

    Tuesday, April 27, 2021 10:22 PM
  • User475983607 posted

    It is very difficult to follow your logic.  When asking for help on a forum it is best if you provided enough code to reproduce the problem.  Below is a working example based on the code you've provided.

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="FormEx.aspx.vb" Inherits="WebFormsVBDemo.FormEx" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server"></asp:GridView>
            </div>
        </form>
    </body>
    </html>
    
    Public Class Data
        Public Property Id As Integer
        Public Property Val As Decimal
    End Class
    
    Public Class FormEx
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                GridView1.DataSource = PopulateData()
                GridView1.DataBind()
            End If
            FormatGridView()
        End Sub
    
        Protected Sub FormatGridView()
            For Each row As GridViewRow In GridView1.Rows
                Dim val As Decimal = Decimal.Parse(row.Cells(1).Text())
                If val <= 0 Then
                    row.Cells(1).BackColor = Drawing.Color.Red
                End If
                If val > 0 And val < 60 Then
                    row.Cells(1).BackColor = Drawing.Color.Orange
                End If
                If val >= 60 Then
                    row.Cells(1).BackColor = Drawing.Color.Yellow
                End If
            Next
        End Sub
    
        Protected Function PopulateData() As List(Of Data)
            Return New List(Of Data) From
                {
                    New Data With {.Id = 1, .Val = -1280},
                    New Data With {.Id = 2, .Val = 33},
                    New Data With {.Id = 3, .Val = 5120},
                    New Data With {.Id = 4, .Val = 0}
                }
        End Function
    
    End Class

    Tuesday, April 27, 2021 11:52 PM
  • User-1716253493 posted

    thepast

    when i  make

    if correctvalue < 0 then

    .......background.color.......

    end if

    its not  reading values  < 0   

       >0    and <90  also  not reading

    correctvalue is not used, it's boolean to indicate the text is a correct decimal value.

    use val variable only, if the text is not correct, val become zero without error message.

    Wednesday, April 28, 2021 5:30 PM