How to display distinct count of column value in TableSummaryRow of WPF DataGrid (SfDataGrid)?
In WPF DataGrid (SfDataGrid) provides support to add summary to display Count, Max, Min, Average and Sum of the records by default. There is no direct aggregate type to display distinct count value of the records. However, it is possible to display distinct count in the summary row by using GridSummaryColumn.CustomAggregate property.
this.dataGrid.TableSummaryRows.Add(new GridTableSummaryRow()
{
ShowSummaryInRow = false,
Position = TableSummaryRowPosition.Top,
SummaryColumns = new ObservableCollection<ISummaryColumn>()
{
new GridSummaryColumn()
{
Name = "CustomerID",
MappingName="CustomerID",
CustomAggregate=new CustomAggregate(),
SummaryType=SummaryType.Custom,
Format="Distinct Count : {DistictCount}"
},
}
});
namespace SfDataGrid_MVVM
{
public class CustomAggregate : ISummaryAggregate
{
public CustomAggregate()
{
}
public double DistictCount { get; set; }
public Action<System.Collections.IEnumerable, string, System.ComponentModel.PropertyDescriptor> CalculateAggregateFunc()
{
return (items, property, pd) =>
{
var enumerableItems = items as IEnumerable<OrderInfo>;
if (pd.Name == "DistictCount")
{
this.DistictCount = enumerableItems.DistictCount<OrderInfo>(q => q.CustomerID);
}
};
}
}
public static class LinqExtensions
{
static List<string> list = new List<string>();
public static double DistictCount<T>(this IEnumerable<T> values, Func<T, string> selector)
{
double ret = 0;
var count = values.Count();
foreach (var value in values)
{
if (!list.Contains((value as OrderInfo).CustomerID))
{
list.Add((value as OrderInfo).CustomerID);
}
}
ret = list.Count;
return ret;
}
}
}
Take a moment to peruse the WPF DataGrid - Summaries, where you can find about Summaries with code examples.
Conclusion
I hope you enjoyed learning about how to display distinct count of a column values in table summary row of DataGrid.
You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF DataGrid documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!