作成日: 2025/04/22 最終更新日: 2025/04/22
文書種別
使用方法
詳細
GridColumnクラスのAggregateプロパティを使用して集計値を表示する場合、既定では「Sum(値)」のように関数名と値がセットで表示されます。
この表示形式から「Sum()」などの関数名を除外して値のみを表示したい場合は、GridCellFactoryクラスを継承し、GetCellContentRenderFragmentメソッドをオーバーライドすることで対応可能です。
【対応手順】
- GridCellFactory を継承したカスタムクラスを作成します。
- GetCellContentRenderFragmentメソッドをオーバーライドし、集計セルの表示内容をカスタマイズします。
- カスタムGridCellFactoryをFlexGridに適用します。
◎サンプルコード
@page "/"
@using C1.DataCollection
@using C1.Blazor.Core
@using C1.Blazor.Grid
@using System.Globalization
@rendermode InteractiveServer
<FlexGrid ItemsSource="items"
AutoGenerateColumns="false"
SelectionMode="GridSelectionMode.Row"
Style="@("height: 50vh; width: 100%")"
GroupHeaderFormat="{value}"
GroupRowStyle="@("background-color: #f0f0f0; font-weight: bold;")"
CellFactory="cf">
<FlexGridColumns>
<GridColumn Binding="Active" Header="Active"></GridColumn>
<GridColumn Binding="Name" Width="GridLength.Star" />
<GridColumn Binding="OrderTotal" Format="n0" Aggregate="GridAggregate.Sum"/>
</FlexGridColumns>
</FlexGrid>
@code {
C1DataCollection<Customer> items;
CellFactory cf = new();
protected override async Task OnInitializedAsync()
{
var dataCollection = new C1DataCollection<Customer>(Customer.GetCustomerList(100));
await dataCollection.GroupAsync(c => c.Country);
items = dataCollection;
}
class CellFactory : GridCellFactory
{
public override RenderFragment GetCellContentRenderFragment(GridCellType cellType,
GridCellRange range)
{
if (Grid.Rows[range.Row] is GridGroupRow &&
cellType == GridCellType.Cell &&
Grid.Columns[range.Column].AggregateFunctions.Count > 0)
{
return builder =>
{
builder.OpenElement(0, "span");
builder.AddContent(1, Grid.GetCellValue(range).ToString());
builder.CloseElement();
};
}
return base.GetCellContentRenderFragment(cellType, range);
}
}
}