【问题标题】:VB.Net Data Grid ViewVB.Net 数据网格视图
【发布时间】:2014-01-01 10:06:26
【问题描述】:

我的代码出现问题。错误中说我应该在代码中添加New,但我不知道该放在哪里。这是函数的完整代码:

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    Try
        Dim TA As New POSCanteenTableAdapters.ItemsTableAdapter
        Dim TB = TA.GetDataByBarcode(TextBox1.Text)
        If TB.Rows.Count = 0 Then
            TextBox2.Text = ""
            TextBox3.Text = ""
            Button1.Enabled = False
            Exit Sub
        End If
        Button1.Enabled = True
        Dim IR As POSCanteen.ItemsRow = TB.Rows(0)
        TextBox2.Text = IR.ItemName
        TextBox3.Text = IR.SellPrice
        Button2.Tag = IR
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly)
    End Try
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim IR As POSCanteen.ItemsRow = Button1.Tag
    Dim RI As New Integer
    Dim ItemLoc As Integer = -1
    For RI = 0 To CDGV3.Rows.Count - 1
        If IR.Barcode = CDGV3.Rows(RI).Cells(0).Value Then
            ItemLoc = RI
            Exit For
        End If
    Next
    If ItemLoc = -1 Then
        CDGV3.Rows.Add(IR.Barcode, IR.ItemName, IR.BuyPrice, IR.SellPrice, 1, IR.SellPrice)
    Else
        Dim ItemCount As Long = CDGV3.Rows(ItemLoc).Cells(4).Value
        ItemCount += 1
        Dim NewPrice As Decimal = IR.SellPrice * ItemCount
        CDGV3.Rows(ItemLoc).Cells(4).Value = ItemCount
        CDGV3.Rows(ItemLoc).Cells(5).Value = NewPrice
    End If
    TextBox1.Text = ""
    TextBox1.Focus()
End Sub

它应该在数据网格中添加一个项目,但不会因为那个错误。我不知道把它放在哪里,因为编译器没有告诉我在哪里。

这是异常消息:Object reference not set to an instance of an object.

【问题讨论】:

  • 它在哪里给出错误? VS 会通过鼠标准确地告诉你哪些东西不是什么东西。
  • 关于 CDGV3.rows.add(IR.ItemName.IR.BuyPrice,Ir.SellPrice,1,Ir.Sellprice)的部分
  • 鼠标悬停在IR变量上是Nothing吗?
  • 打开OPTION STRICT看看有什么问题
  • 那个选项在哪里??

标签: vb.net datagrid


【解决方案1】:

我听不到你的电话,所以我建议这样做。

试试这个

Dim IR As New POSCanteen.ItemsRow = Button1.Tag

Dim ItemCount As New Long = CDGV3.Rows(ItemLoc).Cells(4).Value

Dim NewPrice As New Decimal = IR.SellPrice * ItemCount

【讨论】:

  • 我已经试过了!但它会产生更多错误! :D!该功能适用​​于 Button ! :D!
  • 我把所有的代码!我在 CDGV3.Rows.add(IR.Barcode,IR.ItemName,IR.BuyPrice,IR.SellPrice,1,IR.SellPrice) 中遇到异常
  • 我希望你的问题是在创建对象 IR.. 所以看看它
【解决方案2】:

您需要提前考虑对象不转换的可能性(尤其是当您让编译器猜测时)。不确定这是否是导致问题的对象,但您可能知道。了解如何使用调试器单步调试代码。

Dim IR = TryCast(Button1.Tag, {type})
If Not IR Is Nothing Then
  'rest of your code
End If

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    相关资源
    最近更新 更多