By default, the XtraPivotGrid renders the column headers horizontally. This is fine if we only have a few columns, but chances are there will be too many columns to fit into the limited space available, without resorting to scrolling.
It would be much better, in terms of functionality and aesthetic appeal, if we could render the text at a 45 degree angle, thereby maximizing the number of columns we can fit into a confined space.
Unfortunately the control does not support this directly, but we can work around this.
Firstly, set the ColumnValueLineCount property of the Field being displayed in the Column Area to 2 or 3. This will increase the header band so we have space to render our text diagonally.
Hook into the CustomDrawFieldValue event like so:
private void pivotGrid_CustomDrawFieldValue(object sender,
DevExpress.XtraPivotGrid.PivotCustomDrawFieldValueEventArgs e)
{
HeaderObjectPainter newPainter = e.Painter;
string c = e.Info.Caption;
e.Info.Caption = "";
newPainter.DrawObject(e.Info);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
if (IsColumnHeaderHorizontal(e.Field))
{
Font newFont = new Font(e.Appearance.Font.FontFamily, 8);
e.Appearance.Font = newFont;
StringFormat fmt = new StringFormat();
fmt.Alignment = StringAlignment.Far;
fmt.Trimming = StringTrimming.EllipsisCharacter;
fmt.FormatFlags |= StringFormatFlags.NoWrap;
e.GraphicsCache.DrawString(c, e.Appearance.Font,
e.Appearance.GetForeBrush(e.GraphicsCache),
e.Info.CaptionRect, fmt);
}
else
{
Rectangle newRect = new Rectangle();
newRect = e.Bounds;
newRect.X += newRect.Width;
newRect.Y += newRect.Height;
newRect.Width *= 7;
newRect.Width /= 5;
newRect.Height *= 7;
newRect.Height /= 5;
newRect.Y -= 8;
newRect.Height -= 8;
StringFormat fmt = new StringFormat();
fmt.Alignment = StringAlignment.Far;
fmt.Trimming = StringTrimming.EllipsisCharacter;
fmt.FormatFlags |= StringFormatFlags.NoWrap;
e.GraphicsCache.DrawVString(c, e.Appearance.Font,
e.Appearance.GetForeBrush(e.GraphicsCache),
newRect, fmt, 45);
}
e.Info.InnerElements.DrawObjects(e.Info, e.Info.Cache, Point.Empty);
e.Handled = true;
}
We also need to determine whether to render this field horizontally or not:
private bool IsColumnHeaderHorizontal(PivotGridField field)
{
if (field == null)
{
return true;
}
if (field.Area == PivotArea.RowArea)
{
return true;
}
return false;
}