I'm trying to find a way to check for changes in a DataGridView. The gridview does not have a binding source because I am populating it based on some other information.

Right now I get a NullReferenceException. It happens as the gridview is populated. I have marked the line where this happens.

Here is what I have now...

Private Sub loadTheView
        Dim tblModels As New DataSet1.docHeaderDataTable
        Dim reader As SqlDataReader
        Dim sqlSOPDept As New SqlClient.SqlCommand
        Dim sqlConn As New SqlClient.SqlConnection("Data Source=IMS-SQL-01;Initial Catalog=ImageTraining;Integrated Security=True")
        sqlSOPDept.CommandText = "SELECT DISTINCT model, part, sop, revision FROM [ImageTraining].[dbo].[docHeader] WHERE sop LIKE '" & Convert.ToString(Me.cmbDept.SelectedValue).Replace(" ", "") & "%'"
        sqlSOPDept.CommandType = CommandType.Text
        sqlSOPDept.Connection = sqlConn
        reader = sqlSOPDept.ExecuteReader
        If reader.HasRows Then
            Do While reader.Read()
                Dim rowNum As Integer = 0
                Dim rowTbl As Data.DataRow
                rowTbl = tblModels.NewRow
                rowTbl.Item("model") = reader.Item("model")
                rowTbl.Item("part") = reader.Item("part")
                rowTbl.Item("sop") = reader.Item("sop")
                rowTbl.Item("revision") = reader.Item("revision")
                rowNum += 1
        End If

        'we have data from the reader in a table. set the DataGridView dataSource to that table.
        Me.DataGridView1.DataSource = tblModels

    End Sub

    Private Sub DataGridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
        If Me.DataGridView1.Columns(0).Equals(Nothing) Then
            Me.rwFirst = ""
            Me.rwDGV = Me.DataGridView1.CurrentRow
            'the line below this is where the error occurs. 
            Me.rwIndex = Me.DataGridView1.CurrentRow.Index
            Me.rwFirst = RowtoString(Me.rwIndex)
        End If
    End Sub

    Private Sub DataGridView1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
        Me.rwSecond = RowtoString(rwIndex)
        If String.Compare(rwFirst, rwSecond) Then
            MsgBox("there has been a change")
        End If
    End Sub

    Function RowtoString(ByVal row As Integer) As String
        Dim z As Integer = 0
        Dim text As String
        For z = 0 To DataGridView1.ColumnCount - 1
            text = text & DataGridView1.Item(z, row).Value
        Return text
    End Function

Any help is much appreciated!

5 Years
Discussion Span
Last Post by bklynman01

hmm maybe u can use

me.rwindex = e.rowindex

try this it must be your answer couse i was try.

Edited by Oneryavuz: n/a


Oneryavuz - I'm sorry it took so long for me to reply. The issue gave me enough of a problem that I have decided to just change the way it is done.

I do appreciate the reply. Thanks for the idea!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.