Calculating Column Totals
From the 2009 v1 release, ComponentOne Grid for WPF supports total calculation in the grid. To use totals, set the total bar to be visible and use the Totals property to set up totals calculation.
For the total bar to be visible, the TotalBarVisibility and/or GroupTotalBarVisibility property must be set to Visible. See Setting Total Bar Visibility or Setting Group Total Visibility for more information.
To set up totals calculation, include the totals function within the column's definition. You can set up the column definition to calculate the average, count, sum, minimum, or maximum value in a column. In the following example, column definitions have been set up to calculate different totals in several columns:
<c1grid:C1DataGrid Name="C1DataGrid1" ItemsSource="{Binding Path=ProductsDataSet.Products, ElementName=window, Mode=Default}" AutoGenerateColumns="False" TotalBarVisibility="Visible">
<c1grid:C1DataGrid.Columns>
<c1grid:Column PropertyName="ProductName" Caption="Name" HeaderCellWidth="150"/>
<c1grid:Column PropertyName="QuantityPerUnit" Caption="Unit Size" HeaderCellWidth="125">
<c1grid:Column.Totals>
<c1grid:Total Function="{x:Static c1grid:MaxFunction.Default}" Format="{}{0:0}"/>
</c1grid:Column.Totals>
</c1grid:Column>
<c1grid:Column PropertyName="UnitPrice" Caption="Price" HeaderCellWidth="60">
<c1grid:Column.Totals>
<c1grid:Total Function="{x:Static c1grid:AverageFunction.AverageAll}" Format="{}{0:0.00}"/>
</c1grid:Column.Totals>
</c1grid:Column>
<c1grid:Column PropertyName="UnitsInStock" Caption="In Stock" HeaderCellWidth="75">
<c1grid:Column.Totals>
<c1grid:Total Function="{x:Static c1grid:SumFunction.Default}" Format="{}{0:0}"/>
</c1grid:Column.Totals>
</c1grid:Column>
<c1grid:Column PropertyName="UnitsOnOrder" Caption="On Order" HeaderCellWidth="80">
<c1grid:Column.Totals>
<c1grid:Total Function="{x:Static c1grid:CountFunction.CountNulls}" Format="{}{0:0}"/>
</c1grid:Column.Totals>
</c1grid:Column>
<c1grid:Column PropertyName="ReorderLevel" Caption="Reorder Level" HeaderCellWidth="100">
<c1grid:Column.Totals>
<c1grid:Total Function="{x:Static c1grid:AverageFunction.AverageSkipNulls}" Format="{}{0:0.00}"/>
</c1grid:Column.Totals>
</c1grid:Column>
<c1grid:Column PropertyName="Discontinued" Caption="Discontinued" HeaderCellWidth="95">
<c1grid:Column.Totals>
<c1grid:Total Function="{x:Static c1grid:MinFunction.Default}" Format="{}{0:0}"/>
</c1grid:Column.Totals>
</c1grid:Column>
</c1grid:C1DataGrid.Columns>
</c1grid:C1DataGrid>
The grid will appear similar to the following:
Note that if you do not wish to use one of the built-in functions, you can define your own custom function. For more information about custom functions, see the TotalFunction class description.
|