VB .NET 2008 datagridview对象引用未设置为对象的实例

时间:2015-05-25 14:24:41

标签: vb.net datagridview

使用我的代码接收System.NullReferenceException:

Private Sub dsplay()
        ds.Clear()
        dgv_tran.DataSource = ds
        da = New OleDbDataAdapter("SELECT p.prod_id, p.prod_name, b.cost_ave " & _
                                   "FROM tbl_products p " & _
                                   "INNER JOIN tbl_balance b " & _
                                   "ON p.ID = b.p_id", con)
        da.Fill(dt)
        dgv_tran.DataSource = ds
        dgv_tran.DataMember = "table1"
call dgv_tran_CellClick(Nothing,Nothing)
end sub    

Private Sub dgv_tran_CellClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgv_tran.CellClick
        Try
            Dim i As Integer = dgv_tran.CurrentRow.Index 'error starts in this line
            txtid.Text = dgv_tran.Item(0, i).Value
            txtname.Text = dgv_tran.Item(1, i).Value
            txtcost.Text = dgv_tran.Item(2, i).Value
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

我的datagridview显示了我需要的数据,当我删除 MessageBox.Show(ex.ToString)时我可以继续项目,但我不能忽略这个错误我可以吗? 已经花了4个小时试图找出这个。任何指出问题的帮助都将非常感激。

0 个答案:

没有答案