尝试向datagridview添加行时出错

时间:2016-07-15 05:04:32

标签: vb.net datagridview

您好我确定之前有此工作,但现在我收到以下错误消息:

  

"未处理的类型' System.InvalidCastException'发生了   在Microsoft.VisualBasic.dll" "从字符串转换""输入   '双'无效。"

任何人都可以给我任何关于我出错的提示吗?我似乎无法发现错误

Private Sub btnBasket_Click(sender As Object, e As EventArgs) Handles btnBasket.Click

        Dim Gat As String
        Dim Choc As String
        Dim Cheese As String
        gat = tbGateuQuantity.Text
        Choc = tbChocQuantity.Text
        Cheese = tbCheesecakeQuantity.Text


        If String.IsNullOrEmpty(Gat) Then
            dgvBasket.Rows.Add(New String() {"Strawberry Gateu", lblGateuPrice.Text, tbGateuQuantity.Text, (lblGateuPrice.Text * tbGateuQuantity.Text)})
        Else

        End If

        If String.IsNullOrEmpty(Choc) Then

            dgvBasket.Rows.Add(New String() {"Double Chocolate", lblChocPrice.Text, tbChocQuantity.Text, (lblChocPrice.Text * tbChocQuantity.Text)})
        Else

        End If

        If String.IsNullOrEmpty(Cheese) Then

            dgvBasket.Rows.Add(New String() {"Cheesecake", lblCheesecakePrice.Text, tbCheesecakeQuantity.Text, (lblCheesecakePrice.Text * tbCheesecakeQuantity.Text)})
        Else

        End If
        'adds items to basket

    End Sub

澄清它标记写行并且当任何数量(tbGateuQuantity.text,tbChocQuantity.text和tbCheesecake.text)留空时发生错误,即使它们都被填充它也不会再写行了

1 个答案:

答案 0 :(得分:0)

这可能是因为您使用了标签和文本框的文字属性(例如tbGateuQuantity.Text)吗?当你将它们添加到DataGridView时,这些是字符串,所以将两个相加为小计需要你将它们转换为数字类型,如双重优先,进行计算,然后将答案转换回字符串,这样你就可以了可以将它添加到数组中。

示例 -

If String.IsNullOrEmpty(Gat) Then
    dgvBasket.Rows.Add(New String() {"Strawberry Gateu", lblGateuPrice.Text, tbGateuQuantity.Text, (CStr(CDbl(lblGateuPrice.Text) * CDbl(tbGateuQuantity.Text)))})
End If