如何檢查Datagridview中是否沒有更改,然後提示用戶在我的datagridview中沒有更改。我想在我的更新按鈕中執行此操作,首先檢查是否沒有更改提示msgbox,然後退出sub。怎麼做?可以說數據庫中的數據已經加載到datagridview。如果沒有更改,檢查datagridview中的行和列vb.net
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
'this line is what I want for checking will happen if there is no changes in data happens in datagridview
Dim row, id, dgvUnits As Integer
Dim dgvYearLevel, dgvSemester, dgvCNo, dgvCDescription As String
'Declared this variable to get value event click on dgv
row = DataGridView1.CurrentRow.Index
id = DataGridView1(6, row).Value
dgvYearLevel = DataGridView1(1, row).Value
dgvSemester = DataGridView1(2, row).Value
dgvCNo = DataGridView1(3, row).Value
dgvCDescription = DataGridView1(4, row).Value
dgvUnits = DataGridView1(5, row).Value
Try
con.Open()
With cmd
.Connection = con
.CommandText = "UPDATE tblcurriculumcourses SET YearLevel='" & dgvYearLevel & "', Semester='" & dgvSemester & "', CourseNo='" & dgvCNo & "', CourseDes='" & dgvCDescription & "', Units='" & dgvUnits & "' where PrimaryDummy='" & id & "'"
End With
cmd.ExecuteNonQuery()
MsgBox("Data Has Been Successfully Update!")
Catch ex As Exception
MsgBox("Error when updating data")
End Try
a)打開選項Strict b)始終使用SQL參數而不是串接少量的字符串來創建SQL c)如果您使用數據源,它會告訴您是否有更改d)如果您握住適配器它會爲你更新 – Plutonix
不知道該怎麼做,我只是一個初學者。 –