Search code examples
vb.netdatatabledatagridviewdatasource

The value of the DatagridView's cell appears to change, but it does not


I have a DatagridView whose DataSource is a DataTable. When I change a cell's value at runtime, it appears to be changing well.

However, if I try to assign a value from the DataGridView to a variable by clicking on a row in the DatagridView, the variable gets the original (DataTable) value, not the cell's value.

copy = DataGridView2.Item(7, i).Value

The ReadOnly property of the cells column is false.

Any suggestion?


Solution

  • I was able to solve the problem with this method:

    Private Sub DataGridView2_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellEndEdit
        Dim i As Integer
        i = DataGridView2.CurrentRow.Index
        DataGridView2.DataSource.Rows(i)("copy") = DataGridView2.CurrentCell.Value        
    End Sub
    

    This way, the variable will take on the correct value after I overwrite the value of the cell in the DatagridView.