2016-12-26 57 views
0

如何檢查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 
+0

a)打開選項Strict b)始終使用SQL參數而不是串接少量的字符串來創建SQL c)如果您使用數據源,它會告訴您是否有更改d)如果您握住適配器它會爲你更新 – Plutonix

+0

不知道該怎麼做,我只是一個初學者。 –

回答

0

我沒有一個合法的答案,但我有一個主意。也許,你可以調暗在DataGridView行作爲一個數組或字符串中的一個,後來,按鈕,你可以這樣做

If DataGridView1(1, row).Value = TheArrayOrString Then 
'This is what you want it to do. Example: 
    MsgBox("Your table is the same!") 

也許,嘗試類似的東西,另外,你必須爲DataGridView選擇所有行!它所做的是一旦你將DataGridView中的所有行都作爲一個ArrayList,你可以測試它是否沒有改變。因此,'如果DataGridView1(1,row).Valie = TheArrayOrString'正在測試DataGridView1表在一段時間內是否具有相同的內容。 'MsgBox(「你的桌子是一樣的!」)'是自我解釋。希望這有助於傳播你的想象力!玩的開心! :)

+0

嗯..我不知道該怎麼辦。但你有一個好主意。 –

+0

你可以使用vb代碼先生嗎? –

相關問題