There are two ways to calculate the screen child datagrid totals.
- Directly access the workspace data from the screen child datagrid.
- Use the existing data in screen child datagrid.
Now which method of calculating the totals in the screen child datagrid would you choose? My answer is, it depends on the requirement of the client.
To directly access the workspace data from the screen child datagrid you need to follow the three steps.
- Create a workspace object
- Get the query of the child datagrid
- Loop through the query to compute for the total
Here is the sample code snippet:
Dim ws = Me.Application.CreateDataWorkspace() Dim rs= ws.ApplicationData.MyDataGrid.GetQuery().Execute() Dim records= From r In rs For Each record In records Total = Total + record.Amount Next
To use the existing data in the screen child datagrid you need to follow the following steps:
1. Remove the paging of the data in the datagrid
2. Loop through the datagrid data to compute for the total
Thats it, the problem in method no. 2 is that you need to remove the paging support before you could loop through the data otherwise you can only calculate the data of the current page.
Below is an actual application example that uses method no. 2 in calculating the screen child data grid, of course I already added the screen data so that the result can be displayed in the screen.