Hi All,
I am just trying to update the total quantity in the header section when i update the quantity in the grid.
So, i used this method, In the grid quantity field modified, sum the total quantity for the header and update the quantity in the header total quantity. So, when i close the form and doing some other action. I am getting the below error.
Please can you explain me, what mistake i did.
"Cannot edit a record in MyTable (MyTable).
The values displayed in the form are not current, so an update or deletion cannot be made. To view the current values, on the Command menu, click Restore or press CTRL+F5."
My Code:
public void modified()
{
LineTable lineTable;
real ItemQty;
int TotalNoOfBags;
super();
lineTable = LineTable_ds.getFirst();
ItemQty = 0;
TotalNoOfBags = 0;
while (lineTable)
{
ItemQty += lineTable.ItemQty;
TotalNoOfBags += lineTable.NumberOfBags;
lineTable = LineTable_ds.getNext();
}
HeaderTable.TotalNoOfBags = TotalNoOfBags;
HeaderTable.TotalQty = ItemQty;
HeaderTable.update();
}
Thanks in advance,
Hari
First of all: don’t do it! The line is not saved yet (and doesn’t have to be ever saved), therefore updating the header cause database inconsistency. If you’re lucky, data are inconsistent for short time only (which still can cause troubles) and if you’re not lucky… well, you’ll encounter “mysterious” bugs. It’s nothing what you want in your ERP system. Update the header when saving the line (in update()), in a transaction and count values by SUM().
Also don’t iterate over the form datasource because some records may be filtered out, therefore you would calculate (and save to database) incorrect sums.
Now to your question: It’s not cleat what “MyTable” is; what happens may be something like this: You have a header displayed and cached in the form. Then you change data from code. Then some action tries to save the record loaded to the form, but it fails, because the version in database is newer. AX detects these conflicts, because they could lead to inconsistent data. After updating a record in database, you should reread the buffer that still holds the original version.
Hi Martin,
Thanks for your answer. I have moved the code to line table update method. It is working fine.
But, the total quantity is not displaying when i enter the line qty. Because the total quantity is calculating only on update event.
I need to display the total quantity in the header after the user update the qty in the grid view.
Thanks,
Hari
That’s the normal behavior in AX.
If you really want to do it differently, you can save the record immediately after changing quantity, although it may be impossible, if it’s not ready to be saved (e.g. some mandatory fields are empty).
Or create a separate calculation that will use unsaved values, but don’t put it into database (as explain above), use it just as an indicator how things will look like after committing to database.
I am a rookie in the AX gridview. I frequently feel it so difficult on some gridview header in C# questions. The suggestions here are quite helpful and get some of my curiosity solved. The record in gridview control often troubles me. Fortunately, I always can solve the related problems by myself. Nice to see this question with proper suggestions.
Hi Hari,
Try like this
public void modified()
{
LineTable lineTable;
real ItemQty;
int TotalNoOfBags;
super();
lineTable = LineTable_ds.getFirst();
ItemQty = 0;
TotalNoOfBags = 0;
while (lineTable)
{
ItemQty += lineTable.ItemQty;
TotalNoOfBags += lineTable.NumberOfBags;
lineTable = LineTable_ds.getNext();
}
HeaderTable.TotalNoOfBags = TotalNoOfBags;
HeaderTable.TotalQty = ItemQty;
HeaderTable.write();
HeaderTable_ds.refresh();
}