hi
I have a SfDatagridView, a group summary row, and a CaptionRow.
I need in TableSummaryRow to calculate the total of the (group1-group2) and put the result in TableSummaryRow as you see in the image that I attached.
so: can I get the value of the total of each group?
please consider that I have 2 groups always not more or less.
Hi Mohamad,
We are currently analyzing the reported scenarios. Therefore, we require some time for validation and will provide an update on March 13, 2024.
Regards,
Sreemon Premkumar M.
Hi Mohamad,
We have analyzed your query. Your requirement to obtain the value of each group and load the sum of each group's values into a TableSummaryRow cannot be directly achieved. However, we have provided a workaround by overriding the TableSummaryRow renderer. This involves accessing the group summary values, adding up each group's values, and passing them into the TableSummaryRow renderer.
We have provided a sample for your reference. Kindly refer to the sample and let us know if you have any further concerns regarding this.
Code Snippet:
|
public class CustomGridTableSummaryRenderer : GridTableSummaryCellRenderer { public int GroupSummaryRowValue { get; set; }
public CustomGridTableSummaryRenderer(int groupSummaryValue) { GroupSummaryRowValue = groupSummaryValue; } protected override void OnRender(Graphics paint, Rectangle cellRect, string cellValue, CellStyleInfo style, DataColumnBase column, RowColumnIndex rowColumnIndex) { if (string.IsNullOrEmpty(cellValue)) return; // Creates new number format and apply it to summary value. NumberFormatInfo format = new NumberFormatInfo(); format.NumberDecimalDigits = 1;
//Number format is applied to summary value. cellValue = Convert.ToDouble(double.Parse(GroupSummaryRowValue.ToString(), NumberStyles.Currency)).ToString("N", format); StringFormat stringFormat = new StringFormat(); stringFormat.LineAlignment = StringAlignment.Far; stringFormat.Alignment = StringAlignment.Center; paint.DrawString(cellValue, style.Font.GetFont(), Brushes.Black, cellRect, stringFormat); } }
public Form1() { InitializeComponent(); var groups = sfDataGrid.View.Records.View.Groups; foreach (var group in groups) { if (group != null) { if(((group as Group).Details as GroupRecordEntry).Summaries.Count > 0) { var groupSummaryValue = ((group as Group).Details as GroupRecordEntry).Summaries[0].SummaryValues[0].AggregateValues.ElementAt(0).Value.ToString(); if (int.TryParse(groupSummaryValue, out int intValue)) { summaryValue += intValue; } } } } this.sfDataGrid.CellRenderers.Remove("TableSummary"); this.sfDataGrid.CellRenderers.Add("TableSummary", new CustomGridTableSummaryRenderer(summaryValue));
} |
Image Reference:
|
|
I applied the solution that you sent me:
this is the class:
and I applied:
but nothing is happened, should I do anything more?
Hi Mohamad ,
We have analyzed the image you provided. In that you have
added a custom summary by using the key 'XTotal.' However, you need to pass a
TableSummary key value for the renderer to function correctly.
We have provided a sample for your reference. Kindly review the sample
and let us know if you have any further concerns this.
Code Snippet:
|
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { sfDataGrid.DataSource = new ViewModel().Orders;
GridTableSummaryRow tableSummaryRow1 = new GridTableSummaryRow(); tableSummaryRow1.Name = "TableSummary"; tableSummaryRow1.ShowSummaryInRow = true; tableSummaryRow1.Title = " Total Product Count: {OrderID}"; tableSummaryRow1.Position = VerticalPosition.Bottom;
GridSummaryColumn tableSummaryColumn = new GridSummaryColumn(); tableSummaryColumn.Name = "OrderID"; tableSummaryColumn.SummaryType = SummaryType.DoubleAggregate; tableSummaryColumn.Format = "{Count}"; tableSummaryColumn.MappingName = "OrderID";
tableSummaryRow1.SummaryColumns.Add(tableSummaryColumn);
this.sfDataGrid.TableSummaryRows.Add(tableSummaryRow1);
this.sfDataGrid.ExpandAllGroup();
var groups = sfDataGrid.View.Records.View.Groups; foreach (var group in groups) { if (group != null) { if (((group as Group).Details as GroupRecordEntry).Summaries.Count > 0) { var groupSummaryValue = ((group as Group).Details as GroupRecordEntry).Summaries[0].SummaryValues[0].AggregateValues.ElementAt(0).Value.ToString(); if (int.TryParse(groupSummaryValue, out int intValue)) { summaryValue += intValue; } }
} }
this.sfDataGrid.CellRenderers.Remove("TableSummary"); this.sfDataGrid.CellRenderers.Add("TableSummary", new CustomGridTableSummaryRenderer(summaryValue)); } |