none
calculation of a formula with value in label RRS feed

  • Question

  • hello, my codes is not taking the value of label

    it is taking 0 by default instead of taking label1.text value

      DataGridView9.Rows(i).Cells(16).Value = CDbl(DataGridView9.Rows(i).Cells(13).Value) * Val(Label1.Text)

    please help me in this

    Monday, November 26, 2018 12:26 PM

All replies

  • Try the following

    Dim Value1 = CDbl(DataGridView9.Rows(i).Cells(13).Value)
    Dim Value2 = Val(Label1.Text)
    

    Then 

    DataGridView9.Rows(i).Cells(16).Value = Value1 * Value2

    Put a breakpoint on the line above. When the breakpoint hits hover over both Value1 and Value2, are the values as expected?

    Also you should use Convert.ToInt32 rather than Val. If not sure if DataGridView9.Rows(i).Cells(13).Value is a Double use Integer.TryParse first to validate it's an integer.

    Dim Value1 As Double = 0
    Dim Value2 As Double = CDbl(Label1.Text)
    If Double.TryParse(CStr(DataGridView9.Rows(i).Cells(13).Value), Value1) Then
        DataGridView9.Rows(i).Cells(16).Value = Value1 * Value2
    End If
    Lastly, you might want to check if DataGridView9.Rows(i).Cells(13).Value is not Nothing.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, November 26, 2018 1:09 PM
    Moderator
  • Hello,

    Thanks for the answer but it did not take the value for the first option and for the second one it gave me error message 
    "conversion of string label to type double is not allowed"

    Tuesday, November 27, 2018 12:32 PM
  • Obviously you are trying but not fully.

    In regards to the Label, I left it "as is" for you to figure out, a Label Text property is a string will Val returns a numeric so you need to convert to a string.

    Hint

    CStr(CDbl(Label1.Text))
    CDbl(Label1.Text).ToString

    In regards to not getting the result expected in the first option, what did you learn from using the debugger and setting a breakpoint? What it returns is what you asked for, perhaps you are looking at the wrong column. Bottom line is viewing results in the debugger with a breakpoint does not lie.

     

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, November 27, 2018 1:31 PM
    Moderator
  • DataGridView9.Rows(i).Cells(16).Value = Double.TryParse(DataGridView9.Rows(i).Cells(13).Value) * Double.TryParse(Label1.Text)
    try that.

    • Edited by Xan To Wednesday, November 28, 2018 12:10 AM
    • Proposed as answer by Xan To Wednesday, November 28, 2018 7:01 PM
    Wednesday, November 28, 2018 12:10 AM