This is a migrated thread and some comments may be shown as answers.

Generic Summary Row

3 Answers 72 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Antoine
Top achievements
Rank 1
Antoine asked on 04 Apr 2020, 05:45 AM

Hello,

Is it possible to implement a summary row which would work as such :

Displays sums for every GridViewDecimalColumn independently from their Name attribute.

Thanks in advance

 

3 Answers, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 07 Apr 2020, 09:43 AM

Hello Antoine,

According to the provided information, I can suggest you to create a custom summary item that inherits from the GridViewSummaryItem class. You can override its Evaluate method and introduce your custom logic there. Please refer to the following example:

public RadForm1() { InitializeComponent(); CustomSummaryItem summaryItem = new CustomSummaryItem(); summaryItem.Name = "HourlySalary"; GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem(); summaryRowItem.Add(summaryItem); this.radGridView1.SummaryRowsTop.Add(summaryRowItem); }

publicclassCustomSummaryItem : GridViewSummaryItem { public CustomSummaryItem() { } public CustomSummaryItem(string name, string formatString, GridAggregateFunction aggregate) : base(name, formatString, aggregate) { } public override object Evaluate(IHierarchicalRow row) { decimal sum = 0; foreach (GridViewRowInfo childRow in row.ChildRows) { foreach (var column in childRow.ViewTemplate.Columns) { if (column is GridViewDecimalColumn) { decimal cellValue = Convert.ToDecimal(childRow.Cells[column.Index].Value); sum += cellValue; } } } return sum; }

Note, that in the provided example the summaryItem.Name property is used to specify in which column will be shown the calculated result from the Evaluate method. More information about creating custom GridViewSummaryItem is available here.

This example is a sample demonstration. Feel free to modify it in a way that is most suitable for you.

I hope this helps. Should you have further questions do not hesitate to contact me.

Regards,
Nadya
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Antoine
Top achievements
Rank 1
answered on 07 Apr 2020, 10:31 AM

Hello Nadya, 

 

Thanks for your response.

Is it do-able without specifying the column name ?

 

 

0
Nadya | Tech Support Engineer
Telerik team
answered on 10 Apr 2020, 07:46 AM

Hello Antoine,

The GridViewSummaryItem.Name property is needed because RadGridView should somehow know where exactly to show the returned result. If you do not set it you can see that the summary row is displayed but it is empty. So, it is not possible to show the value without specifying the Name of the column where it should be displayed. 

If you do not know the name of your columns you can access the column by index from the RadGridView.Columns collection and thus you can get its name:

CustomSummaryItem summaryItem = new CustomSummaryItem();
summaryItem.Name = this.radGridView1.Columns[6].Name;

I hope this helps. Should you have other questions do not hesitate to contact me.

Regards,
Nadya
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
GridView
Asked by
Antoine
Top achievements
Rank 1
Answers by
Nadya | Tech Support Engineer
Telerik team
Antoine
Top achievements
Rank 1
Share this question
or