0

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?

Pawel
  • 141
  • 1
  • 10
  • In the posted `PL_DGV_CellValueChanged` event, I do not see any code that is related to “adding a red border” to the edited cell. It appears to be logging this change into another data table but nothing in relation to coloring the cell’s border. I assume the UN-used `new_style` variable is an attempt? – JohnG Jul 26 '21 at 08:23
  • 1
    Also, I could be mistaken about this, however, the line of code… `Dim grid_row As DataRow = Me.DataSet.PL.Rows(e.RowIndex)` … looks suspicious. If `Me.DataSet.PL` is a data source to the grid… then… `e.RowIndex` … may not necessarily be the same “row” index in the underlying data table. Example, if the grid is sorted or filter, then the two row indexes will have a good chance of being different. I would think you would want something like… `Dim grid_row As DataRowView = PL_DGV.Rows(e.RowIndex).DataBoundItem` …Note the `DataRowView`. – JohnG Jul 26 '21 at 08:24
  • Lastly, it is unknown why or what fields/cells you need to keep track of if they change, however, since it “appears” the grid is using a `DataTable`, are you aware of the `DataTable`’s `GetChanges` function. Granted, it may not go down to the cell level, however it does go down to the row level. IN other words, `GetChanges` will return another data table with the rows that have changed since the tables `AcceptChanges` was last called. Just a thought. – JohnG Jul 26 '21 at 08:25
  • Thank you for the advice on changing the line to `Dim grid_row As DataRowView = PL_DGV.Rows(e.RowIndex).DataBoundItem`, it makes sense and I never thought of it, although never occurred an issue caused by using it the way I did. `new_style` variable was an attempt which totally failed as I had no clue what I was doing and was merely checking what functions are available within the `DataGridViewStyle` to figure something out on my own. In terms of the `GetChanges` and `AcceptChanges` I couldn't get those to do what I needed in terms of saving changes, but I managed with the way I show it above. – Pawel Jul 27 '21 at 07:12

2 Answers2

1

refer this link How do you draw a border around a DataGridView cell while it's being edited?

or you can try this but need to imporve code

 Dim rec As New Drawing.Rectangle

            rec = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False)

            DataGridView1.CreateGraphics.DrawRectangle(New Pen(Color.Red, 2), CInt(rec.X), CInt(rec.Y), CInt(rec.Width - 2), CInt(rec.Height - 2))

            DataGridView1.EditingControl.BackColor = Color.Red
Yogesh
  • 180
  • 1
  • 10
  • Before it errors, I can see it drawing the border on the cell but then it errors on the line `DataGridView1.EditingControl.BackColor = Color.Red` with message `Object reference not set to an instance of an object.`. All I have edited is changed `DataGridView1` to my correct DGV and changed the first two lines to one `Dim rec As Rectangle = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False)` – Pawel Jul 26 '21 at 12:03
  • On the last line `DataGridView1.EditingControl.BackColor = Color.Red`. If I comment this line out it only changes the colour of the border on top of the cell to red, both sides and bottom border of the cell are not changed. – Pawel Jul 27 '21 at 06:57
  • It is working fine for me. Did you set datatable to datagridview or you add columns to Datagridview? If you are adding columns to datagridview make sure it is textbox .. Correct me if I am wrong – Yogesh Jul 27 '21 at 07:51
  • Not sure what you mean. The DataGridView is bound to a DataTable within a DataSet and displays data from that bount DataTable. If I type in `DataGridView1.` I do not see a function like `EditingControl` within the popup in Visual Studio 2019. – Pawel Jul 27 '21 at 10:10
  • 1
    May be it is because of version of .Net, Need to read more about ```EditingControl``` property – Yogesh Jul 28 '21 at 03:47
0

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
Pawel
  • 141
  • 1
  • 10