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.
Hi Mike,
I'm having the same issue. Could you please provide the code in VB? I'm new to Infragistics and don't know how to convert your code to VB. Thank you
Hi Steve,
Sure, here's a VB version of the same code:
Public Class MiddleAlignDrawFilter Implements IUIElementDrawFilter Public Function DrawElement(drawPhase As Infragistics.Win.DrawPhase, ByRef drawParams As Infragistics.Win.UIElementDrawParams) As Boolean Implements Infragistics.Win.IUIElementDrawFilter.DrawElement Dim cell As UltraGridCell = drawParams.Element.GetContext(GetType(UltraGridCell)) If Not cell Is Nothing AndAlso cell.Column.Key = "String 1" Then drawParams.AppearanceData.TextVAlign = VAlign.Middle End If Return False End Function Public Function GetPhasesToFilter(ByRef drawParams As Infragistics.Win.UIElementDrawParams) As Infragistics.Win.DrawPhase Implements Infragistics.Win.IUIElementDrawFilter.GetPhasesToFilter If TypeOf drawParams.Element Is EditorWithTextDisplayTextUIElement Then Return DrawPhase.BeforeDrawForeground End If Return DrawPhase.None End Function End Class
You assign the DrawFilter to the grid like this:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Me.UltraGrid1.DrawFilter = New MiddleAlignDrawFilter() End Sub
Thank you very much for your help. You're awsome.
Thanks Mike! This solution totally works. I needed to sort the content in a merged cell vertically centered and this code snippet you provided did it!
Thank you for this solution.
But when I put .Style = ColumnStyle.Integer is don't work
Francois