在If条件中将datagrid中的多个值转换为字符串

时间:2014-04-29 19:05:59

标签: vb.net datagrid

我有一个带有组合框的数据网格。这个组合框中有数字和文字。我想提出一个条件,如果组合框文本是BOGO或免费的,那么我的变量dblQty应为0。

我能够让它适用于一个条件BOGO,(见下面的代码),但我似乎无法让它与2个条件一起工作,我试过BOGO Or Complimentary但我得到了无法转换为布尔错误。

Private Sub grdNewInvoice_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles grdNewInvoice.CurrentCellDirtyStateChanged

    'Procedure runs when the combobox in the datagrid changes. 
    'The procedure calculates the amount * the quantity in the datagrid
    'It also updates the label with the total

    'Local variables
    Dim dblQty As Double
    Dim dblPrice As Double

    'Calculate the amount * quantity
    For index As Integer = 0 To grdNewInvoice.RowCount - 1

        If grdNewInvoice.Rows(index).Cells(1).Value.ToString = "BOGO" Then

            dblQty = 0
            dblPrice = Convert.ToDouble(grdNewInvoice.Rows(index).Cells(2).Value)

            grdNewInvoice.Rows(index).Cells(3).Value = dblPrice * dblQty

        Else

            dblQty = Convert.ToDouble(grdNewInvoice.Rows(index).Cells(1).Value)
            dblPrice = Convert.ToDouble(grdNewInvoice.Rows(index).Cells(2).Value)

            grdNewInvoice.Rows(index).Cells(3).Value = dblPrice * dblQty

        End If

    Next

    'Update the total label.
    PublicSubs.totalDataGrid()

End Sub

1 个答案:

答案 0 :(得分:1)

基于I tried BOGO Or Complimentary帖子中传递的评论,您可能已经尝试过这样做:

If grdNewInvoice.Rows(index).Cells(1).Value.ToString = "BOGO" 
          Or "Complimentary" Then

OR不起作用。第二个测试可以是完全不同的东西,例如

If thisThing = "BOGO" Or Sale.BlackFriday = True Then ...

试试这个:

Dim thing As String = grdNewInvoice.Rows(index).Cells(1).Value.ToString 

If thing = "BOGO" Or Thing = "Complimentary" Then...