我有一个 CellValueChanged 事件,我将任何更新的记录添加到审计表中,并且我想在此事件中将单元格边框更改为红色以指示单元格已更新:
Private Sub PL_DGV_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles PL_DGV.CellValueChanged
If isLoaded Then
Dim grid_row As DataRow = Me.DataSet.PL.Rows(e.RowIndex)
Dim Column1 = Variable1
Dim Column2 = grid_row.Item("Column2").ToString().Trim()
Dim Column3 = grid_row.Item("Column3").ToString().Trim()
Dim Updated_column_name = Me.DataSet.PL.Columns(e.ColumnIndex).ColumnName
Dim Updated_value = grid_row.Item(Updated_column_name).ToString()
Dim row As DataRow = Me.DataSet.PL_ChangesLog.NewRow()
row("Column1") = Column1
row("Column2") = Column2
row("Column3") = Column3
row("Column4") = Updated_column_name
row("Column5") = Updated_value
row("timestamp") = DateTime.Now()
row("username") = Environment.UserName()
Me.DataSet.PL_ChangesLog.Rows.Add(row)
Dim new_style = New DataGridViewCellStyle
unsaved_changes = True
End If
End Sub
此外,一旦保存更改,这些单元格将需要再次删除它们的边框以恢复默认值。这将通过 Button 或 Form 正在关闭并且用户选择“是”时发生:
Private Sub PLC_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If unsaved_changes Then
Dim result = MsgBox("There are unsaved changes within the grid - would you like to save changes?", MessageBoxButtons.YesNoCancel)
If result = DialogResult.Yes Then
ElseIf result = DialogResult.Cancel Then
e.Cancel = True
End If
End If
End Sub
是否可以在 CellValueChanged 事件中完成,还是应该根据更新的单元格索引作为单独的函数完成?