Generic Summary Row

4 posts, 0 answers
  1. Antoine
    Antoine avatar
    2 posts
    Member since:
    Mar 2020

    Posted 04 Apr Link to this post

    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

     

  2. Nadya
    Admin
    Nadya avatar
    344 posts

    Posted 07 Apr Link to this post

    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.
  3. Antoine
    Antoine avatar
    2 posts
    Member since:
    Mar 2020

    Posted 07 Apr in reply to Nadya Link to this post

    Hello Nadya, 

     

    Thanks for your response.

    Is it do-able without specifying the column name ?

     

     

  4. Nadya
    Admin
    Nadya avatar
    344 posts

    Posted 10 Apr Link to this post

    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.
Back to Top