7

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.

user4340666
  • 1,453
  • 15
  • 36

2 Answers2

6

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;
        }
    }
user4340666
  • 1,453
  • 15
  • 36
0

To work correctly with RightToLeft languages you should set CSS style rtl. For example:

private void CustomizeCellsInThirdColumn()
{
    int thirdColumn = 2;
    DataGridViewColumn column =
        dataGridView.Columns[thirdColumn];
    DataGridViewCell cell = new DataGridViewTextBoxCell();

    cell.Style.Direction = "rtl";
    column.CellTemplate = cell;
}

For more information read CSS direction Property and DataGridViewCell.Style Property

direction: rtl;

Alexander Dayan
  • 2,846
  • 1
  • 17
  • 30