Search code examples
c#right-to-leftdatagridviewtextboxcell

RightToLeft DatagridviewCell


How can i sert the property RightToLeft to a DatagridviewCell? I tried to set

Alignement property to "MiddleRight" but since my DatagridviewCell value is

Arabic and English it is not displayed as i want from right to left.


Solution

  • I found a solution with Cell_Painting event and it works. This is the code:

    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex == 2 && e.RowIndex >= 0)
            {
                e.PaintBackground(e.CellBounds, true);
                TextRenderer.DrawText(e.Graphics, e.FormattedValue.ToString(), e.CellStyle.Font, e.CellBounds, e.CellStyle.ForeColor, TextFormatFlags.RightToLeft | TextFormatFlags.Right);
                e.Handled = true;
            }
        }