Example E2084
Visible to All Users

WinForms Data Grid - Print group summaries under corresponding columns

This example creates a custom grid control that prints group summary items under corresponding columns:

WinForms Data Grid - Print group summaries under corresponding columns

Important

The example uses internal APIs that may change in new versions.

Files to Review

Does this example address your development requirements/objectives?

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

Example Code

Q250602/MyGridControl.cs(vb)
C#
using System; using System.Drawing; using DevExpress.XtraGrid; using DevExpress.XtraPrinting; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Base; using DevExpress.XtraGrid.Registrator; using DevExpress.XtraGrid.Views.Printing; using DevExpress.Utils; using DevExpress.XtraGrid.Views.Grid.ViewInfo; using DevExpress.XtraGrid.Drawing; using System.Collections.Generic; namespace DXSample { public class MyGridControl :GridControl { public MyGridControl() : base() { } protected override void RegisterAvailableViewsCore(InfoCollection collection) { base.RegisterAvailableViewsCore(collection); collection.Add(new MyGridViewInfoRegistrator()); } } public class MyGridView :GridView { public MyGridView() : base() { } public MyGridView(GridControl grid) : base(grid) { } internal const string MyGridViewName = "MyGridView"; protected override string ViewName { get { return MyGridViewName; } } protected override BaseViewPrintInfo CreatePrintInfoInstance(PrintInfoArgs args) { return new MyGridViewPrintInfo(args); } } public class MyGridViewInfoRegistrator :GridInfoRegistrator { public MyGridViewInfoRegistrator() : base() { } public override string ViewName { get { return MyGridView.MyGridViewName; } } public override BaseView CreateView(GridControl grid) { return new MyGridView(grid); } } public class MyGridViewPrintInfo :GridViewPrintInfo { public MyGridViewPrintInfo(PrintInfoArgs args) : base(args) { } protected virtual void DrawGroupTextBrick(BrickGraphics graph, Rectangle bounds, int rowHandle) { string prefFormat = View.GroupFormat; View.GroupFormat = "{0}:{1}"; string groupText = View.GetGroupRowDisplayText(rowHandle); View.GroupFormat = prefFormat; Rectangle groupTextBounds = new Rectangle(bounds.X + Indent, bounds.Y, ((PrintColumnInfo)Columns[0]).Bounds.Width / 2 - Indent, bounds.Height); DrawTextBrick(graph, groupText, groupTextBounds, false); for (int i = 0; i < Columns.Count; i++) { PrintColumnInfo col = (PrintColumnInfo)Columns[i]; Rectangle groupCellBounds = col.Bounds; groupCellBounds.Y = bounds.Y; groupCellBounds.Height = bounds.Height; if (i == 0) { groupCellBounds.X = groupCellBounds.Width / 2; groupCellBounds.Width -= groupCellBounds.Width / 2; } GridGroupSummaryItem summary; string summaryText = string.Empty; if (printSummaryInfo.TryGetValue(col.Column.FieldName, out summary)) summaryText = View.GetGroupSummaryDisplayText(rowHandle, summary); TextBrick summaryBrick = (TextBrick)DrawTextBrick(graph, summaryText, groupCellBounds, false); if (summary != null && View.Columns[summary.FieldName].VisibleIndex == 0) summaryBrick.HorzAlignment = HorzAlignment.Far; summaryBrick.Padding = new PaddingInfo(5, 2, 0, 0); } } protected override void PrintGroupRow(int rowHandle, int level) { Rectangle r = Rectangle.Empty; r.X = Indent + level * ViewViewInfo.LevelIndent; r.Width = this.fMaxRowWidth - r.Left; r.Y = Y; r.Height = CurrentRowHeight; SetDefaultBrickStyle(Graph, Bricks["GroupRow"]); DrawGroupTextBrick(Graph, r, rowHandle); Y += r.Height; } public override void PrintRows(BrickGraphics graph) { PreparePrintSummaryInfo(); base.PrintRows(graph); } private Dictionary<string, GridGroupSummaryItem> printSummaryInfo = new Dictionary<string, GridGroupSummaryItem>(); private void PreparePrintSummaryInfo() { printSummaryInfo.Clear(); foreach (GridGroupSummaryItem item in View.GroupSummary) if (item.ShowInGroupColumnFooter == null && string.IsNullOrEmpty(item.ShowInGroupColumnFooterName)) printSummaryInfo.Add(item.FieldName, item); } } }
Q250602/Form1.cs(vb)
C#
using System; using System.Windows.Forms; namespace Q250602 { public partial class Form1 :Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'nwindDataSet.Order_Details' table. You can move, or remove it, as needed. this.order_DetailsTableAdapter.Fill(this.nwindDataSet.Order_Details); } private void OnShowPrintPreviewButtonClick(object sender, EventArgs e) { orderDetailsGrid.ShowPrintPreview(); } private void OnExportToXlsButtonClick(object sender, EventArgs e) { orderDetailsGrid.ExportToXls(@"..\..\order_details.xls"); //orderDetailsGrid.ExportToHtml(@"..\..\order_details.html"); //orderDetailsGrid.ExportToMht(@"..\..\order_details.mht"); //orderDetailsGrid.ExportToPdf(@"..\..\order_details.pdf"); //orderDetailsGrid.ExportToRtf(@"..\..\order_details.rtf"); //orderDetailsGrid.ExportToText(@"..\..\order_details.txt"); //orderDetailsGrid.ExportToPdf(@"..\..\order_details.pdf"); } } }

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.