Search code examples
.netwinformsdatagridviewright-to-leftdatagridviewcolumn

How can I change text direction of a column in datagridview?


How can I set text direction of an specific column in datagridview without changing whole datagridviews direction? (I mean text direction change for right to left languages and it's not same as text-alignment)


Solution

  • I know it's not the best answer but temporary I have handled CellPainting event to change text direction of that specific column:
    (in my example datagridview is RightToLeft and I marked column 5 to be painted in LeftToRight direction)

    Public Sub DGV_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DGV.CellPainting
        If e.ColumnIndex = 5 And Not Object.Equals(e.Value, DBNull.Value) Then
            e.PaintBackground(e.CellBounds, False)
            TextRenderer.DrawText(e.Graphics, e.Value, e.CellStyle.Font, e.CellBounds, e.CellStyle.ForeColor, e.CellStyle.BackColor, (TextFormatFlags.Left Or TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter) And (Not TextFormatFlags.RightToLeft))
            e.Handled = True
        End If
    End Sub
    

    but still I'm looking for a more comfortable trick/solution to do this because this solution still lacks when editing the text and also handling paint event is not showing very beautiful results as I expect.