What Does "commit" Mean

RogerSchlueter-7899 1,446 Reputation points
2024-01-11T20:37:13.9333333+00:00

I have a DataGrid with a DataTable as the ItemsSource. Leaving out columns irrelevant to this post the DataTable is:

MyTable Columns.Add("TransactionID", GetType(Integer))
MyTable Columns.Add("Amount", GetType(Decimal))

and the DataGrid (again, leaving other columns) is:

<DataGrid>
	<DataGrid.Columns>
		<DataGridTextColumn Binding="{Binding TransactionID}"
	Amount, StringFormat=C2}"
	</DataGrid.Columns>
</DataGrid>

I keep a running sum of the "Amount" values as rows are added to the table as follows:

Private Sub MyGrid_RowEditEnding(sender As Object, e As DataGridRowEditEndingEventArgs) Handles MyGrid.RowEditEnding
    RunningSum = CDec(MyTable.Compute("SUM(Amount)", ""))
End Sub

However this does not work because even though the row being added is "committed", it apparently is not actually in the table. That is, the running sum does not include the "Amount" in the row just added. What is the right way to do this?

Developer technologies | VB
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2024-01-12T02:47:42.7233333+00:00

    Hi @RogerSchlueter-7899,

    It seems like the issue you're facing is related to the timing of when the RowEditEnding event is triggered in relation to when the row is actually added to the DataTable. The RowEditEnding event might be fired before the changes are fully committed to the DataTable.

    To ensure that you include the newly added row in your running sum calculation, you can try using the RowEditEnding event to update the running sum after the changes have been committed. You can achieve this by handling the RowEditEnding event and then updating the running sum within the Dispatcher to postpone it until after the current event has been processed.

    Private Sub MyGrid_RowEditEnding(sender As Object, e As DataGridRowEditEndingEventArgs) Handles MyGrid.RowEditEnding
    
        Dispatcher.BeginInvoke(Sub()
                                   RunningSum = CDec(MyTable.Compute("SUM(Amount)", ""))
                               End Sub, DispatcherPriority.ContextIdle)
    End Sub
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.