Category / Section
How to resolve the text position in edit mode when the text vertical alignment is in middle?
1 min read
Resolve the moving text position
The text position getting changed in edit mode is the default behavior of Grid Control. The difference in text position is viewable when the vertical alignment is set to middle and the row height is increased.
Solution
This is because, even the row height has been changed, and the text position remains the same. To fix this, you have to set the position of cell text depending on the row height in QueryCellInfo event.
C#
//Form Load //increase the rowheight this.grid.DefaultRowHeight = 35; //Set the vertical alignment to Middle this.grid.TableStyle.VerticalAlignment = GridVerticalAlignment.Middle; this.grid.QueryCellInfo += new GridQueryCellInfoEventHandler(gridControl1_QueryCellInfo); void gridControl1_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e) { if (e.Style.CellType == GridCellTypeName.TextBox && this.grid.CurrentCell.HasCurrentCellAt(e.RowIndex,e.ColIndex)) { //to adjust the text position in edit mode. e.Style.TextMargins.Top = System.Convert.ToInt32(this.grid.RowHeights[e.RowIndex] / 3); } }
VB
'Form Load 'increase the rowheight Me.grid.DefaultRowHeight = 40 'Set the vertical alignment to Middle Me.grid.TableStyle.VerticalAlignment = GridVerticalAlignment.Middle AddHandler grid.QueryCellInfo, AddressOf gridControl1_QueryCellInfo Private Sub gridControl1_QueryCellInfo(ByVal sender As Object, ByVal e As GridQueryCellInfoEventArgs) 'to adjust the text position in edit mode. If e.Style.CellType Is GridCellTypeName.TextBox AndAlso Me.grid.CurrentCell.HasCurrentCellAt(e.RowIndex, e.ColIndex) Then e.Style.TextMargins.Top = sSystem.Convert.ToInt32(Me.grid.RowHeights(e.RowIndex) / 3) End If End Sub
Screenshot:
Samples: