Row and Column Totals in Callback Mode
TrueGrid Sample
Here's a small app demonstrating row and column totals in call back (Application) mode using TrueGrid Pro.
We'll start with three TrueGrid tables. You could just use one and a few Select Case statements for row and column, but I prefer to use three for readability and code segregation.
LISTING ONE Sub Form_Load () Dim I As Integer table1.Rows = 5 table1.Columns = 5 For I = 1 To table1.Columns table1.ColumnSumEnable(I) = True Next I table2.Rows = 5 table2.Columns = 1 table2.ColumnSumEnable(1) = True Table3.Rows = 1 Table3.Columns = 5 Table3.ColumnCellAttrs(6) = True End Sub
No data control? Correct - just make sure the .DataMode property is set to 1 - Application and set up table1 columns as editable. Set up table2 and table3 columns as not editable. For a table1 of 5 rows and 5 columns, we must have an array for data in each cell. I didn't like this at first as I felt TrueGrid should keep up with its own data. But trust me - you'll need this array! If you can figure out a way to do without it, please tell me. So :
Dim DataArray(5,5)As Variant
Now we need to do some initialisation, and I prefer the form load event (Listing One). We first set up the rows and columns or we won't be able to enter data, and then we need to tell TrueGrid to keep up with column totals - a little tricky!
You
could leave the background colour as white but I like the 3D look.
One other trick with column totals. On the ColumnSumChanged event, make sure you refresh the row, not the entire grid.
If you refresh the grid, the entire grid flashes, which doesn't look too good. In this case, we'll be updating table3 from the table1 ColumnSumChanged event.
Next we need to deal with fetching our data. The fetch event is fired for every cell but don't let this throw you. DataArray is handled in Update event. Value is current cell value of Row and Column :
value = DataArray(Row,Col)
On the other side of the fetch coin is, of course, update. Once our user has finished typing in a value in one of the cells on grid we need to save the value - that's what DataArray is for:
Sub Table1_Update (Row As Long, Col As Integer, value As String) DataArray(Row, Col) = value table2.RefreshRow = Row End Sub
Now we need to make sure our row sums (in table2) are correct. Be careful here - this is the trickiest event as it uses another cool TrueGrid property - referencerow. Setting it does not affect .rowindex or .columnindex. So we step through each cell using a loop. Referencerow tells us what row to use, ColumnText (column) tells us what column.
I hide the zero values, because I like the look. Refer to code below:
Sub Table2_Fetch (Row As Long, Col As Integer, value As String)
Dim I As Integer
Dim Rowsum As Double table1.ReferenceRow = Row For I = 1 To table1.columns
Rowsum = Rowsum + Val(table1.ColumnText(I))
Next I
If Rowsum = 0 Then
value = ""
Else
value = "" & Rowsum
End If
End Sub
Finally we need to update our column sums. This is almost too easy as we have that wonderful gift from TrueGrid, ColumnSum. We simply use that to update our sums, with a bit of special handling to account for the fact that our sixth column is in fact the sum of table2. Refer below:
Sub Table3_Fetch (Row As Long, Col
As Integer, value As String)
If Col < 6 Then
value = table1.ColumnSum(Col)
If Val(value) = 0 Then
value = ""
End If
Else ' 6th column:table2
value = table2.ColumnSum(1)
If Val(value) = 0 Then
value = ""
End If
End If
End Sub
Apex Software is releasing the OCX version of TrueGrid Pro in December 1995, priced at $299. The upgrade from TrueGrid Pro 2.1 will be priced at $189, upgrade from TrueGrid Standard will be $259. For further sales and technical details please drop us a note at orders@gui.com.au or contact GUI Computing on (03) 9804 3999.
![]()