windowsforms-3028-controls-and-libraries-data-grid-examples-painting-how-to-custom-draw-footer-cells.md
WinForms Grid - How to Custom Draw Footer Cells.
The following example demonstrates how to paint footer cells.
using System;
using System.Data;
using System.Drawing;
using System.ComponentModel;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using DevExpress.Data;
using DevExpress.XtraGrid;
using DevExpress.LookAndFeel;
namespace DXApplication {
public partial class Form1 : DevExpress.XtraEditors.XtraForm {
public Form1() {
InitializeComponent();
gridControl1.DataSource = TaskObject.GetData();
gridControl1.ForceInitialize();
gridView1.OptionsView.ShowFooter = true;
gridView1.Columns["Memory"].Summary.Add(
new GridColumnSummaryItem(SummaryItemType.Sum, "Memory", "{0:n} MB"));
gridView1.CustomDrawFooterCell += GridView1_CustomDrawFooterCell;
}
private void GridView1_CustomDrawFooterCell(object sender, DevExpress.XtraGrid.Views.Grid.FooterCellCustomDrawEventArgs e) {
int dx = e.Bounds.Height;
Brush brush = e.Cache.GetSolidBrush(DXSkinColors.ForeColors.Information);
Rectangle r = e.Bounds;
r.Inflate(-1, -1);
e.Cache.FillRectangle(brush, r);
r.Inflate(-6, 0);
e.Appearance.ForeColor = Color.White;
e.Appearance.DrawString(e.Cache, e.Info.DisplayText, r);
e.Handled = true;
}
}
public class TaskObject {
public string AppName { get; set; }
[DisplayFormat(DataFormatString = "p")]
public double CPU { get; set; }
[DisplayFormat(DataFormatString = "n")]
public double Memory { get; set; }
public double Network { get; set; }
public static List<TaskObject> GetData() {
return new List<TaskObject> {
new TaskObject(){ AppName = "Microsoft Teams", CPU = 0.018, Memory = 402.4, Network = 0.1 },
new TaskObject(){ AppName = "Microsoft Visual Studio 2022", CPU = 0.046, Memory = 1309.2, Network = 0 },
new TaskObject(){ AppName = "Visual Studio Code", CPU = 0.03, Memory = 104.2, Network = 2.1 },
new TaskObject(){ AppName = "Microsoft Edge", CPU = 0.078, Memory = 644.7, Network = 10.2 },
};
}
}
}
Imports System
Imports System.Data
Imports System.Drawing
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.ComponentModel.DataAnnotations
Imports DevExpress.Data
Imports DevExpress.XtraGrid
Imports DevExpress.LookAndFeel
Namespace DXApplication
Partial Public Class Form1
Inherits DevExpress.XtraEditors.XtraForm
Public Sub New()
InitializeComponent()
gridControl1.DataSource = TaskObject.GetData()
gridControl1.ForceInitialize()
gridView1.OptionsView.ShowFooter = True
gridView1.Columns("Memory").Summary.Add(New GridColumnSummaryItem(SummaryItemType.Sum, "Memory", "{0:n} MB"))
AddHandler gridView1.CustomDrawFooterCell, AddressOf GridView1_CustomDrawFooterCell
End Sub
Private Sub GridView1_CustomDrawFooterCell(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.FooterCellCustomDrawEventArgs)
Dim dx As Integer = e.Bounds.Height
Dim brush As Brush = e.Cache.GetSolidBrush(DXSkinColors.ForeColors.Information)
Dim r As Rectangle = e.Bounds
r.Inflate(-1, -1)
e.Cache.FillRectangle(brush, r)
r.Inflate(-6, 0)
e.Appearance.ForeColor = Color.White
e.Appearance.DrawString(e.Cache, e.Info.DisplayText, r)
e.Handled = True
End Sub
End Class
Public Class TaskObject
Public Property AppName() As String
<DisplayFormat(DataFormatString := "p")>
Public Property CPU() As Double
<DisplayFormat(DataFormatString := "n")>
Public Property Memory() As Double
Public Property Network() As Double
Public Shared Function GetData() As List(Of TaskObject)
Return New List(Of TaskObject) From {
New TaskObject() With {.AppName = "Microsoft Teams", .CPU = 0.018, .Memory = 402.4, .Network = 0.1},
New TaskObject() With {.AppName = "Microsoft Visual Studio 2022", .CPU = 0.046, .Memory = 1309.2, .Network = 0},
New TaskObject() With {.AppName = "Visual Studio Code", .CPU = 0.03, .Memory = 104.2, .Network = 2.1},
New TaskObject() With {.AppName = "Microsoft Edge", .CPU = 0.078, .Memory = 644.7, .Network = 10.2}
}
End Function
End Class
End Namespace
The following example demonstrates how to customize the text displayed in a footer cell based on a specific condition:
private void GridView1_CustomDrawFooterCell(object sender, FooterCellCustomDrawEventArgs e) {
if (e.Column.FieldName == "Bytes")
e.Info.DisplayText = ((int)e.Info.Value / 1024).ToString() + " MBs";
}
Private Sub GridView1_CustomDrawFooterCell(ByVal sender As Object, ByVal e As FooterCellCustomDrawEventArgs)
If e.Column.FieldName = "Bytes" Then
e.Info.DisplayText = (DirectCast(e.Info.Value, Integer) \ 1024).ToString() & " MBs"
End If
End Sub