Example E1581
Visible to All Users

Winforms Data Grid - Calculate a summary against detail rows and display it in a master row cell

This example creates an unbound column in the master GridView and handles the CustomUnboundColumnData event to calculate the number of detail rows and display the number in the master row.

Winforms Data Grid - Calculate a summary against detail rows and display it in a master row cell

C#
public Form1() { // ... gridControl1.ForceInitialize(); GridColumn col = gridView1.Columns.AddField("Tasks"); col.UnboundType = DevExpress.Data.UnboundColumnType.Integer; col.Visible = true; gridView1.CustomUnboundColumnData+=new DevExpress.XtraGrid.Views.Base.CustomColumnDataEventHandler(gridView1_CustomUnboundColumnData); } private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { GridView view = sender as GridView; if (e.Column.FieldName != "Tasks") return; if (!e.IsGetData) return; DataRow row = ((view.DataSource as IList)[e.ListSourceRowIndex] as DataRowView).Row; e.Value = row.GetChildRows("Project_Tasks").Length; }

Files to Review

Documentation

Does this example address your development requirements/objectives?

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

Example Code

WindowsApplication59/Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Columns; namespace WindowsApplication59 { public partial class Form1 : Form { public Form1() { InitializeComponent(); InitData(); gridControl1.UseEmbeddedNavigator = true; gridControl1.ForceInitialize(); GridColumn col = gridView1.Columns.AddField("Tasks"); col.UnboundType = DevExpress.Data.UnboundColumnType.Integer; col.Visible = true; gridView1.CustomUnboundColumnData+=new DevExpress.XtraGrid.Views.Base.CustomColumnDataEventHandler(gridView1_CustomUnboundColumnData); } private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { GridView view = sender as GridView; if (e.Column.FieldName != "Tasks") return; if (!e.IsGetData) return; DataRow row = ((view.DataSource as IList)[e.ListSourceRowIndex] as DataRowView).Row; e.Value = row.GetChildRows("Project_Tasks").Length; } void InitData() { dataTable1.Rows.Add(new object[] { 0, "Software", 4000 }); dataTable1.Rows.Add(new object[] { 1, "Hardware", 1500 }); dataTable1.Rows.Add(new object[] { 2, "Project X", 100000 }); dataTable2.Rows.Add(new object[] { null, 0, "Drivers" }); dataTable2.Rows.Add(new object[] { null, 0, "Application" }); dataTable2.Rows.Add(new object[] { null, 1, "All the stuff" }); dataTable2.Rows.Add(new object[] { null, 2, "Part X" }); dataTable2.Rows.Add(new object[] { null, 2, "Part Y" }); dataTable2.Rows.Add(new object[] { null, 2, "Part Z" }); } } }

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.