我在vb.net中编码并使用DataGridView。
我只需要使用一个阿拉伯语的列,所以我不希望我的整个datagridview使用从右到左的文本方向。
我只想将一个特定的列调整为从右到左的文本方向。
到目前为止我正在使用此代码
Private Sub gv_items_CellPainting(sender As System.Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles gv_items.CellPainting
'This is for Arabic Text with Numeric Chars Alignments... AKA RTL for COL = 13 = Notes
If e.ColumnIndex = 13 AndAlso e.RowIndex >= 0 Then
e.PaintBackground(e.CellBounds, True)
TextRenderer.DrawText(e.Graphics, e.FormattedValue.ToString(), e.CellStyle.Font, e.CellBounds, e.CellStyle.ForeColor, TextFormatFlags.RightToLeft And TextFormatFlags.Right)
e.Handled = True
End If
End Sub
Private Sub gv_items_EditingControlShowing(sender As System.Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles gv_items.EditingControlShowing
'This is for Arabic Text with Numeric Chars Alignments... AKA RTL for COL = 13
If (gv_items.CurrentCell.ColumnIndex = 13) Then
e.Control.RightToLeft = RightToLeft.Yes
End If
End Sub
这个代码在我编辑单元格时工作正常但是一旦编辑了单元格,它就不能正确显示阿拉伯文本和数字。
请看一下这个截图
感谢您的帮助。
答案 0 :(得分:0)
无需使用CellPainting
事件进行文本对齐。
DataGridView控件具有特定属性(Alignment Enum
),可以说要对齐的列右边是索引2中的列,你可以这样做:
' Align all cells of column 2 to the right
DataGridView1.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
' alighn column header to the right
DataGridView1.Columns(2).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight