In the InitializeLayout event of a grid, I set the e.Layout.Bands[bandName].Columns[columnName].Width of each column, and I set
e.Layout.Override.RowSizing = RowSizing.AutoFree; e.Layout.Override.CellMultiLine = DefaultableBoolean.True;
The text automatically wraps if it is not wide enough to fit in a column, and the row height adjusts automatically. However the text in all cells is aligned to the top of the cell. I think the grid would look a lot better if all text were centered vertically instead.
I have tried setting
e.Layout.Override.CellAppearance.TextVAlign = VAlign.Middle;
and several variants of this, but I have been unable to find a way to make the text in all cells center vertically, although the text in some of them, for example in the Date columns, does do so. There must be a way, but what is it?
When a cell in the grid goes into edit mode, the grid displays an Inbox TextBox over the cell to allow you to edit. The inbox TextBox control does not support vertically aligning the text when multiline is set to true. So the grid can't support this, either.
If your cell is not editable and you want to center it when it is not in edit mode, you can do that with a DrawFilter. Let me know if that's the case and I will see if I can dig up the DrawFilter code for you.
OK, if you can pass the Draw Filter code on, I'll give it a try.
I wonder if the Draw Filter will also work when we print or export to PDF using UltraGridDocumentExporter? I'm interected in vertically centering multiline text in that case also.
The draw fitler is pretty simple. You just check for the element that draws that text and then check the column key to make sure it's the column you want. I guess you could check the column.CellAppearance.TextVAlign instead of the column key if you want to do this in a more generic way. Anyway, here's a little sample drawfilter to get you started:
public class MiddleAlignDrawFilter : IUIElementDrawFilter { #region IUIElementDrawFilter Members bool IUIElementDrawFilter.DrawElement(DrawPhase drawPhase, ref UIElementDrawParams drawParams) { UltraGridCell cell = drawParams.Element.GetContext(typeof(UltraGridCell)) as UltraGridCell; if (cell != null && cell.Column.Key == "String 1") drawParams.AppearanceData.TextVAlign = VAlign.Middle; return false; } DrawPhase IUIElementDrawFilter.GetPhasesToFilter(ref UIElementDrawParams drawParams) { if (drawParams.Element is EditorWithTextDisplayTextUIElement) return DrawPhase.BeforeDrawForeground; return DrawPhase.None; } #endregion }
This should work for printing, also - the same drawfilter will get called when the grid prints. But it won't work for exporting the PDF.
You could also change the column style to FormattedText and prefix your cell text with a paragraph tag, e.g:
<p align="center">
Though technically the attibute is for horizontal alignment, it seems to work around the vertical alignment problem when using CellMultiLine
I'm pretty sure you could get the PDF exporter to work by handling an event like CellExporting and exporting the cell text yourself. It would be a bit tricky, though, especially if you have an image in the cell. But it should be possible for you to cancel the default exporting of the text and export it yourself vertically centered. Assuming that the Documents engine supports that.
Thank, MIke. I will try this.
If it doesn't work when exporting to PDF with the UltraGridDocumentExporter, then that is yet another limitation of that class that tends to make it less than useful.I will find out.