vb.netwinformsdatagridviewdatagridviewcellstyle

How to add red border to edited cell in DataGridView after CellValueChanged Event


I have a CellValueChanged event where I add any updated records into an audit table, and I'd like to change the cell border to red to indicate the cell has been updated, within this event:

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

Also, once the changes are saved, these cells would need their border removed again back to default. This would occur via Button or while Form is closing and user selects "Yes":

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

Is it possible to have it done within the CellValueChanged Event or should it be done as a separate function based on updated cell's indexes?


Solution

  • I haven't managed to figure out how to properly draw a rectangle around a cell, but after further discussion with the provider of the requirements it is set that the row which had a cell edited within it will be set to Bold font.

    Achieved it with the following code:

        Dim DGV_row As DataGridViewRow = PL_DGV.Rows(e.RowIndex)
        Dim new_style As New DataGridViewCellStyle With {
            .Font = New System.Drawing.Font("Segoe UI", 9, FontStyle.Bold)
        }
        DGV_row.DefaultCellStyle = new_style