<SfGrid DataSource="@Products" AllowGrouping="true" AllowPaging="true">
<GridGroupSettings Columns=@Units></GridGroupSettings>
<GridAggregates>
<GridAggregate>
<GridAggregateColumns>
<GroupCaptionTemplate>
@{
var aggregate = (context as AggregateTemplateContext);
<div>
@*<p>Count: @aggregate.Count</p>*@
<p>Calaculated Count: - @GetCaculatedAggregate()</p>
</div>
}
</GroupCaptionTemplate>
</GridAggregateColumn>
<FooterTemplate>
@{
var aggregate = (context as AggregateTemplateContext);
<div>
<p>Count: @aggregate.Count</p>
</div>
}
</FooterTemplate>
</GridAggregateColumn>
</GridAggregateColumns>
</GridAggregate>
</GridAggregates>
<GridColumns>
. ..
</GridColumns>
</SfGrid>
@code{
public List<Product> Products { get; set; }
private string[] Units = (new string[] { "QuantityPerUnit" });
public string GetCaculatedAggregate()
{
// Here, we can calculate custom aggregate operations and return the result
return Queryable.Count(Products.Select(x => x).AsQueryable()).ToString();
}
. ..
} |
<SfGrid DataSource="@Products" AllowGrouping="true" AllowPaging="true">
<GridGroupSettings Columns=@Units></GridGroupSettings>
<GridAggregates>
<GridAggregate>
<GridAggregateColumns>
@foreach (var prop in typeof(Product).GetProperties())
{
<GroupCaptionTemplate>
@{
var aggregate = (context as AggregateTemplateContext);
<div>
<p>Calaculated Count: - @GetCaculatedAggregate()</p>
</div>
}
</GroupCaptionTemplate>
</GridAggregateColumn>
}
</GridAggregateColumns>
</GridAggregate>
</GridAggregates>
<GridColumns>
<GridColumn Field=@nameof(Product.ProductName) HeaderText="Product Name" TextAlign="TextAlign.Right" Width="220"></GridColumn>
. . .
</GridColumns>
</SfGrid> |
<GridGroupSettings Columns=@Template>
<CaptionTemplate>
@{
var order = (context as CaptionTemplateContext);
<div>@order.HeaderText: @order.Key - @order.Count items</div>
}
</CaptionTemplate>
</GridGroupSettings>
<SfGrid DataSource="@Products" AllowGrouping="true" AllowPaging="true">
<GridGroupSettings Columns=@Units>
<CaptionTemplate>
@{
var order = (context as CaptionTemplateContext);
<div>@GetData(order)</div>
}
</CaptionTemplate>
</GridGroupSettings>
<GridColumns>
<GridColumn Field=@nameof(Product.ProductName) HeaderText="Product Name" TextAlign="TextAlign.Right" Width="220"></GridColumn>
<GridColumn Field=@nameof(Product.QuantityPerUnit) HeaderText="Quantity Per Unit" Width="150"></GridColumn>
<GridColumn Field=@nameof(Product.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn>
<GridColumn Field=@nameof(Product.UnitsInStock) HeaderText="Units In Stock" TextAlign="TextAlign.Right" Width="130"></GridColumn>
<GridColumn Field=@nameof(Product.Discontinued) HeaderText="Discontinued" TextAlign="TextAlign.Right" DisplayAsCheckBox="true" Type="ColumnType.Boolean"></GridColumn>
</GridColumns>
</SfGrid>
@code{
public List<Product> Products { get; set; }
private string[] Units = (new string[] { "Status", "Severity" });
public string GetData(CaptionTemplateContext args)
{
if (args.Field == nameof(Product.Status)) // for foreignkeycolumn
{
var val = Products.GroupBy(x => x.Status).Where(X => X.Key == args.Key).Select(x => x).ToList();
return $"{args.Field} : {args.Key} - {val.First().Count().ToString()} items";
}
else
{
return $"{args.Field} : {args.Key} - {args.Count.ToString()} items";
}
}
|
The latest solution gives the count of all records ignoring the grouping. The count should match the number of records displayed in that group and sub-groups. While searching for a solution again, I came across a Syncfusion Knowledge Article that describes what I'm after. It can be found at Customize the group caption text | WinForms - SfDataGrid (syncfusion.com). How do I implement this on the blazor side?
<SfGrid DataSource="@Products" AllowGrouping="true" AllowPaging="true">
<GridGroupSettings Columns=@Units>
<CaptionTemplate>
@{
var order = (context as CaptionTemplateContext);
<div>@GetData(order)</div>
}
</CaptionTemplate>
</GridGroupSettings>
<GridColumns>
<GridColumn Field=@nameof(Product.ProductName) HeaderText="Product Name" TextAlign="TextAlign.Right" Width="220"></GridColumn>
. ..
</GridColumns>
</SfGrid>
@code{
public List<Product> Products { get; set; }
private string[] Units = (new string[] { "Status", "Severity" });
public string GetData(CaptionTemplateContext args)
{
if (args.Field == nameof(Product.Status))
{
var val = Products.GroupBy(x => x.Status).Where(X => X.Key == args.Key).Select(x => x).ToList();
return $"{args.Field} : {args.Key} - {val.First().Count().ToString()} items - Count: {args.Count}";
}
else
{
return $"{args.Field} : {args.Key} - {args.Count.ToString()} items - Count: {args.Count}";
}
}
. . .
} |
You do realize this is the same solution sent on June 7. Please take the time to read what I'm after. From the first email I thought it was very clear. On October 4, I included a link to an existing Syncfusion Knowledge article that shows exactly what I'm after. The solutions provided so far doesn't accomplish what I'm after. In the knowledge article there is a screenshot that can be use as a reference. I would like that exact output.
Article: Customize the group caption text | WinForms - SfDataGrid (syncfusion.com)
public string GetData(CaptionTemplateContext args)
{
if (args.Field == nameof(Product.Status))
{
var val = Products.GroupBy(x => x.Status).Where(X => X.Key == args.Key).Select(x => x).ToList();
return $"{args.Field} : {args.Key} - {val.First().Count().ToString()} records ({args.Count} Sub Groups)";
}
else
{
return $"{args.Field} : {args.Key} - {args.Count.ToString()} records ({args.Count} Sub Groups)";
}
} |
|
|
I added a 3rd grouping in the results become skewed. The image below shows Sev - High with 2 records; however, there are actually 3 records.
Also, if I add the below lines of codes to GetData. The "Severity High 7 " shows a count of 7 but there are only 3 matching that grouping.
else if (args.Field == nameof(Product.Severity))
{
var val = Products.GroupBy(x => x.Severity).Where(X => X.Key == args.Key).Select(x => x).ToList();
return $"{args.Field} : {args.Key} - {val.First().Count().ToString()} records ({args.Count} Sub Groups)";
}
If I flip-flop Status and Severity grouping, the result are skewed.
I increased the count from 10 to 20 to get more records to work with.
<SfGrid DataSource="@Products" AllowGrouping="true" AllowPaging="true">
<GridGroupSettings EnableLazyLoading="true" Columns=@Units>
<CaptionTemplate>
@{
var order = (context as CaptionTemplateContext);
<div>@GetData(order)</div>
}
</CaptionTemplate>
</GridGroupSettings>
<GridColumns>
<GridColumn Field=@nameof(Product.ProductName) HeaderText="Product Name" TextAlign="TextAlign.Right" Width="220"></GridColumn>
. ..
</GridColumns>
</SfGrid>
|
|
Still getting the same result. The Sub-Group count doesn't match the number of actual sub-groups. For example, The first line should be 'Status: New - 10 records (3 Sub Groups)
public string GetData(CaptionTemplateContext args)
{
if (args.Field == nameof(Product.Status))
{
var val = Products.GroupBy(x => x.Status).Where(X => X.Key == args.Key).Select(x => x).ToList();
var InnerCount = val.First().GroupBy(x => x.Severity).Select(x => x).ToList();
return $"{args.Field} : {args.Key} - {val.First().Count().ToString()} records - { InnerCount.Count()} subitems";
}
else
{
return $"{args.Field} : {args.Key} - {args.Count.ToString()} records";
}
} |
After realizing AggregateTemplateContext shared similar properties (i.e. HeaderText, Key, etc...) found in CaptionTemplateContext, I was able to resolve my issue. AggregateTemplateContext contains the counts I wanted and rolls up correctly along with the text I wanted to display.
<GridAggregate>
<GridAggregateColumns>
<GridAggregateColumn Field=@nameof(Product.QuantityPerUnit) Type="AggregateType.Count">
<GroupCaptionTemplate>
@{
var aggregate = (context as AggregateTemplateContext);
<div>@aggregate.HeaderText: @aggregate.Key - @aggregate.Count items</div>
}
</GroupCaptionTemplate>
</GridAggregateColumn>
</GridAggregateColumns>
</GridAggregate>
I hid the default group caption by doing the following...
<GridGroupSettings Columns=@Units>
<CaptionTemplate />
</GridGroupSettings>