Search code examples
vb.netdatagridviewdatagridviewrow

If DataRow exists in DataTable, update values within it


Each time a user changes a value in one of the editable columns within my DataGridView, it triggers a recalculation of up to 8 fields. This means the event CellValueChanged Event on the DataGridView will trigger more than 1 time.

I want to create a row in a DataSet.DataTable which will include the new values for all columns after the user has changed a value.

The issue I am having is that the code below creates a row for each column that has had its value changed due to the recalculated columns.

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 unsaved_row As DataRow = grid_row

            Me.DataSet.PL_UnsavedChanges.ImportRow(unsaved_row)

            unsaved_changes = True
        End If
    End Sub

How could I make it so that if a row already exists, it will just update the values within it each time?

There is no PrimaryKeys assigned to the DataTable within the DataSet, but there are 3 columns which would be used as unique values per row.

[UPDATE BASED ON COMMENTS]

With the code shown above what will happen is that the user will update a value in column 1 >

Event is triggered and new row is created which will include the new value in Column 1 >

Column 2 is recalculated based on changes in Column 1 >

Event is triggered and new row is created which will include the new value in Column 1 and Column 2 >

Column 3 is recalculated based on changes on either of the previously mentioned columns >

Event is triggered and new row is created which will include new value in Column 1, Column 2 and Column 3.

What I want to achieve is to have 1 row which will have all the new values, no need to generate any other rows but one which includes the changes from the user action and any recalculations based on that.


Solution

  • After the discussion within the comments the solution to my issue, or rather what I did to overcome it was the below:

    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("price_list") = Price_list
    row("warehouse") = Warehouse
    row("product_code") = Product
    row("column_name") = Updated_column_name
    row("updated_value") = Updated_value
    row("timestamp") = DateTime.Now()
    row("username") = Environment.UserName()
    
    Me.DataSet.PL_ChangesLog.Rows.Add(row)
    
    unsaved_changes = True
    

    This allows me to create a DataTable holding a row of data for each cell that has had its value updated, whether recalculated via code or by user action.

    Based on that I can pick out the last audit and use that as the "Save Changes" to commit the changes to the SQL Database.