Click here to Skip to main content
15,037,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Datagridview binded to bindingsoure

after I do manipulation on the data in bindingsource and refresh the datagridview some cells value is changed, I want to know which cell is changed

'ID Name Age << DATA
' 1 ABC   17
' 2 DEF   20
' 3 GHI   30

Dim dt = GetDataFromSqlServerForTableCustomer()
Dim bs As New BindingSource
bs.DataSource = dt

dgv.DataSource = bs




'If I code this for example
bs(2)("Age") = 33
bs.ResetBindings(False)
'row(2).cell(2) will changed and became 33

'ID Name Age << DGV
' 1 ABC   17
' 2 DEF   20
' 3 GHI   33



'If I code this for example
bs(1)("Age") = 25
bs.ResetBindings(False)
'row(1).cell(2) will changed and became 25
'ID Name Age << DGV
' 1 ABC   17
' 2 DEF   25
' 3 GHI   30


I want to capture which cell in the dgv is changed and make its backcolor to red
How to capture which cell is changed?

What I have tried:

CellValueChanged or CellValueNeeded are not triggerd when dgv cell value changed
Posted
Updated 17-Apr-21 0:49am
Comments
Jo_vb.net 17-Apr-21 5:37am
   
CellValueChanged or CellValueNeeded are not triggerd when you refresh the datagridview.

Try manipulation on the data in the datagridview.
Samir Ibrahim 17-Apr-21 6:34am
   
@jo_vb.net : That will be a little hard since I give an example, but my actual need requite finding max depending on criteria so my actual manipulation is on the data

1 solution

I Solve it using a collection, but I have to loop twice to all datagridview cells

Dim dt = GetDataFromSqlServerForTableCustomer()
Dim bs As New BindingSource
bs.DataSource = dt
dgv1.DataSource = bs

coll = New Collection

'Read DGV into Collection
For i = 0 To dgv1.Rows.Count -1
   For j = 0 To dgv1.ColumnCount -1
        coll.Add(dgv1(j,i).Value,i.ToString+j.ToString)
   Next
Next

' DO Data Manipulation
bs.ResetBindings(False)

' Compare DGV after update with Collection
For i = 0 To dgv1.Rows.Count - 1
    For j = 0 To dgv1.ColumnCount - 1
        If coll(i.ToString+j.ToString) <> dgv1(j,i).Value
            dgv1(j,i).Style.BackColor = Color.MistyRose
        End If
    Next
Next


I wonder if there is better way to do it.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900