未将对象引用设置为DATAGRID中的对象实例 [英] Object reference not set to an instance of an object in DATAGRID

查看:123
本文介绍了未将对象引用设置为DATAGRID中的对象实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

您能帮我解决这个错误吗?
对象引用未设置为对象的实例."

我的代码是这样的:
我声明这很薄.

hello

Can you help me in this error:
"Object reference not set to an instance of an object."

My code is this:
I declare this thins.

Public txt_PK As TextBox
    Public txt_ID As TextBox
    Public txt_Name As TextBox
    Public txt_ContactNo As TextBox
    Public txt_YrSctn As TextBox
    Public txt_Adviser As TextBox
    Public txt_Address As TextBox



并在此处出现错误的地方(如果是datagrid的话).



and in here where the error appear, in this event of datagrid.

Private Sub dtgView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dtgView.KeyDown
        If e.KeyCode = Keys.Enter Then
            Try
                'i = e.rowIndex
                'j = e.columnIndex
                Dim a As Integer
                a = dtgView.CurrentRow.Index
                If dtgView.SelectedRows.Count > 0 Then
                    txt_ID.Text = dtgView.Item(1, a).Value
                    txt_Name.Text = dtgView.Item(4, a).Value
                    txt_ContactNo.Text = dtgView.Item(7, a).Value
                    txt_YrSctn.Text = dtgView.Item(5, a).Value
                    txt_Adviser.Text = dtgView.Item(6, a).Value
                    txt_Address.Text = dtgView.Item(8, a).Value

                    txt_ID.Select()
                    Me.Close()
                End If
                If dtgView.SelectedRows Is Nothing OrElse dtgView.SelectedRows.Count = 0 Then
                    'Return
                End If

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.OkOnly, My.Application.Info.Title)
            End Try
        End If
    End Sub

推荐答案

在我看来,dtgView.CurrentRow.Index 中的dtgViewdtgView.CurrentRow为空.
To me it looks like dtgView or dtgView.CurrentRow in dtgView.CurrentRow.Index is null.




只需按以下步骤更改代码即可.

Hi,

Just change your code as follows.

Private Sub dtgView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dtgView.KeyDown
        If e.KeyCode = Keys.Enter Then
            Try
                'i = e.rowIndex
                'j = e.columnIndex

                Dim a As Integer
If dtgView.SelectedRows Is Nothing OrElse dtgView.SelectedRows.Count = 0 Then
              Return
              End If

                a = dtgView.CurrentRow.Index
                If dtgView.SelectedRows.Count > 0 Then
                    txt_ID.Text = dtgView.Item(1, a).Value
                    txt_Name.Text = dtgView.Item(4, a).Value
                    txt_ContactNo.Text = dtgView.Item(7, a).Value
                    txt_YrSctn.Text = dtgView.Item(5, a).Value
                    txt_Adviser.Text = dtgView.Item(6, a).Value
                    txt_Address.Text = dtgView.Item(8, a).Value

                    txt_ID.Select()
                    Me.Close()
                End If
              

            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.OkOnly, My.Application.Info.Title)
            End Try
        End If
    End Sub



上面的修改将解决您的问题.

问候,
Kiran.



The above modification will solve your problem.

regards,
Kiran.


数据网格示例中的值 [
Values from a datagrid examples[^]


But what you need to do is something like this, but you will need replace iRowNumber for the row that you want and iColNumber for the column Number

dim strValue as string = ""

if datagridview1.SelectedRows.Count > 0 Then
  strValue = datagridview1.Cells(iRowNumber, iColNumber).value
end if


这篇关于未将对象引用设置为DATAGRID中的对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆