在删除particuler行时,我们得到空引用异常 [英] on deleting the particuler row we get the null referance exception

查看:66
本文介绍了在删除particuler行时,我们得到空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生,我在datagridview中获取了表数据,然后选择了paticuler单元并进行了更新,然后可以轻松进行工作.
但是当我删除particuler行时,我从wr(下一个聚焦行的行索引)中得到了异常
在我的项目中,必须将重点放在下一行上,并且还需要选择整行删除.
我需要一些指导来阻止这种异常
我的代码在下面

Dear sir I get the table data in datagridview and then select the paticuler cell and update it then working is easily go on .
But when i delete the particuler row then then i got the exception from wr(row index of next focusing row)
In my project the The focus on the next row is necessary and select the whole row delete is also necessary.
I want some guidance for handlind this exception
My code is below

'Imports msdatasrc
Imports System.Data.OleDb
Public Class Form1
    Dim connetionString As String
    Dim connection As OleDbConnection
    Dim adapter As OleDbDataAdapter
    Dim cmdBuilder As OleDbCommandBuilder
    Dim ds As New DataSet
    Dim changes As DataSet
    Dim sql As String
    Dim i As Int32
    'Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    '    Try
    '        cmdBuilder = New OleDbCommandBuilder(adapter)
    '        changes = ds.GetChanges()
    '        If changes IsNot Nothing Then
    '            adapter.Update(changes)
    '        End If
    '        MsgBox("Changes Done")
    '    Catch ex As Exception
    '        MsgBox(ex.ToString)
    '    End Try
    'End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        connetionString = "Provider=SQLOLEDB;Data Source=I7;Integrated Security=SSPI;Initial Catalog=BGBC2012"
        connection = New OleDbConnection(connetionString)
        sql = "select * from st"
        Try
            adapter = New OleDbDataAdapter(sql, connection)
            adapter.Fill(ds)
            DataGridView1.DataSource = ds.Tables(0)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub DataGridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
        Try
            Dim wr As Integer
            Dim wc As Integer
            cmdBuilder = New OleDbCommandBuilder(adapter)
            changes = ds.GetChanges()            
            If changes IsNot Nothing Then    
                wr = DataGridView1.CurrentRow.Index //From this line we get the exception
                wc = DataGridView1.CurrentCell.ColumnIndex
                adapter.Update(changes)
                changes.AcceptChanges()
                ds.AcceptChanges()
                DataGridView1.CurrentCell = DataGridView1.Item(wc, wr + 1)
                adapter = New OleDbDataAdapter(sql, connection)
                adapter.Fill(ds)
                DataGridView1.DataSource = ds.Tables(0)
                'MsgBox("Changes Done")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

推荐答案

If changes IsNot Nothing Then
              wr = DataGridView1.CurrentRow.Index //From this line we get the exception
              wc = DataGridView1.CurrentCell.ColumnIndex
              adapter.Update(changes)
              changes.AcceptChanges()
              ds.AcceptChanges()
              DataGridView1.CurrentCell = DataGridView1.Item(wc, wr + 1)
              adapter = New OleDbDataAdapter(sql, connection)
              adapter.Fill(ds)
              DataGridView1.DataSource = ds.Tables(0)
              'MsgBox("Changes Done")
          End If



替换



replace

If changes IsNot Nothing Then
      If DataGridView1.CurrentRow IsNot Nothing  then
              wr = DataGridView1.CurrentRow.Index //From this line we get the exception
              wc = DataGridView1.CurrentCell.ColumnIndex
              adapter.Update(changes)
              changes.AcceptChanges()
              ds.AcceptChanges()
              DataGridView1.CurrentCell = DataGridView1.Item(wc, wr + 1)
              adapter = New OleDbDataAdapter(sql, connection)
              adapter.Fill(ds)
              DataGridView1.DataSource = ds.Tables(0)
              'MsgBox("Changes Done")
      End if
End If



祝您编码愉快!
:)



Happy Coding!
:)


这篇关于在删除particuler行时,我们得到空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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