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
sqlConn.Open()
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")
tblModels.Rows.Add(rowTbl)
rowNum += 1
Loop
End If
reader.Close()
'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
Else
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
Next
Return text
End Function
Any help is much appreciated!