Example E2488
Visible to All Users

WinForms Data Grid - How to calculate a total summary based on other columns' summary values

The GridView.CustomSummaryCalculate event does not allow you to obtain total summaries calculated against other columns. This example handles the GridView.CustomDrawFooterCell and GridView.CustomDrawGroupRowFooterCell events to calculate total summary values based on other columns' total summaries and display custom summaries within footer and group footer cells.

Files to Review

Documentation

See Also

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Q273845/Form1.cs(vb)
C#
using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Views.Grid; using System; using System.Data; using System.Windows.Forms; namespace Q273845 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { productsBindingSource.DataSource = GetProductsDataTable(); } DataTable GetProductsDataTable() { DataTable table = new DataTable(); table.TableName = "Products"; table.Columns.Add(new DataColumn("ProductName", typeof(string))); table.Columns.Add(new DataColumn("UnitsInStock", typeof(int))); table.Columns.Add(new DataColumn("UnitPrice", typeof(double))); table.Columns.Add(new DataColumn("Category", typeof(string))); Random random = new Random(); for(int i = 0; i < 20; i++) { int index = i + 1; table.Rows.Add("Product " + index, random.Next(1, 20), Math.Round(random.NextDouble() * 1000, 3), "Category " + i % 7); } return table; } private void gridView1_CustomDrawFooterCell(object sender, FooterCellCustomDrawEventArgs e) { if(e.Column == colPrice) e.Info.DisplayText = (Convert.ToDecimal(colUnitPrice.SummaryItem.SummaryValue) * Convert.ToDecimal(colUnitsInStock.SummaryItem.SummaryValue)).ToString(); } private void gridView1_CustomDrawRowFooterCell(object sender, FooterCellCustomDrawEventArgs e) { if(e.Column == colPrice) { GridView view = (GridView)sender; e.Info.DisplayText = (Convert.ToDecimal(view.GetGroupSummaryValue(e.RowHandle, (GridGroupSummaryItem)view.GroupSummary["UnitPrice"])) * Convert.ToDecimal(view.GetGroupSummaryValue(e.RowHandle, (GridGroupSummaryItem)view.GroupSummary["UnitsInStock"]))).ToString(); } } private void gridView1_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e) { GridView view = (GridView)sender; if(e.Column == colPrice) if(e.IsGetData) e.Value = Convert.ToDecimal(view.GetListSourceRowCellValue(e.ListSourceRowIndex, colUnitPrice)) * Convert.ToInt32(view.GetListSourceRowCellValue(e.ListSourceRowIndex, colUnitsInStock)); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.