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

Adding GridViewSummary totals at runtime

1 Answer 57 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kerry
Top achievements
Rank 1
Kerry asked on 23 Nov 2012, 03:33 PM
Hello, 

I have RadGridView for Winforms 2012 Q1, it has the AutoGenerateColumns property set to True so the number of columns is dictated by the datasource.  I'd like to add totals to gridview for the numeric data, I can do this at design time using the SummaryRowsBottom property/collection but since I do not know how many columns will be display I need to do this at runtime.  I do know that the columns I want to Sum start at column 2 to TotalNumOfColumns.  So I can iterate through the columns collection ...I'm just not sure which event is the best to add the summary nor the syntax.

Thanks

1 Answer, 1 is accepted

Sort by
0
Accepted
Kerry
Top achievements
Rank 1
answered on 23 Nov 2012, 06:33 PM
Hello, I have solved this myself after I bind the datasource to the grid, if there is a better way please comment.


private void AddSummaryTotals()
        {
            if (this.rgvResults.SummaryRowsBottom != null)
                this.rgvResults.SummaryRowsBottom.Clear();
 
            //Since our grid is dynamic we can't do this at design time, so we see what columns have been generated and build from that.
            GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem();
            for (int index = 2; index < rgvResults.Columns.Count; index++)
            {
                GridViewSummaryItem summaryItem = new GridViewSummaryItem();
                summaryItem.Name = rgvResults.Columns[index].Name;
                summaryItem.Aggregate = GridAggregateFunction.Sum;
                if (this.rbHours.Checked)
                    summaryItem.FormatString = "{0:N}"; //fixed numeric
                else
                    summaryItem.FormatString = "{0:C}"; //currency
                summaryRowItem.Add(summaryItem);
            }
 
            this.rgvResults.SummaryRowsBottom.Add(summaryRowItem);
 
        }
Tags
GridView
Asked by
Kerry
Top achievements
Rank 1
Answers by
Kerry
Top achievements
Rank 1
Share this question
or