4

How do I set the RightToLeft property for a particular DataGridViewCell in a DataGridViewColumn?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
mbmsit
  • 63
  • 1
  • 7
  • 1
    I need to set Direction not Alignment for cell – mbmsit Feb 17 '11 at 09:50
  • What does that mean? I'm not sure that I understand the distinction. As my answer explains, you cannot set the RightToLeft property for individual cells; it only applies to the entire control. It's designed for locales using right-to-left fonts, in which case everything on the screen would need to be right-to-left, not just one particular cell. How is "direction" different than "alignment"? – Cody Gray - on strike Feb 19 '11 at 04:35
  • (I know this is old) The use of right-to-left fonts doesn't necessary mean that everything on the screen would needs to be right-to-left. For example you could have a DataGridView that is displaying translations of words in different langs. The column showing a RTL lang should be displayed RTL. It seems that when using a .net DataGridView to achieve this one has to set the cell font to a RTL and set CellStyle.Alignment to DataGridViewContentAlignment.MiddleRight. (although this doesn't seem to work using mono winforms) – Tom Sep 17 '12 at 17:04
  • Does this answer your question? [RightToLeft DatagridviewCell](https://stackoverflow.com/questions/28959661/righttoleft-datagridviewcell) – Sayed Abolfazl Fatemi Nov 02 '20 at 22:01

5 Answers5

5

I know it's an old question, but as others said, DataGridViewCell and DataGridViewColumn don't have the RightToLeft property. However, there are workarounds to this problem:

  1. Handle the CellPainting event and use the TextFormatFlags.RightToLeft flag:

    private void RTLColumnsDGV_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
       {
          if (e.ColumnIndex == RTLColumnID && 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;
           }
       }

    (Code taken from CodeProject question.)

  2. If it's only one particular cell in the DGV, you may try to insert the invisible RTL character (U+200F) in the beginning of the cell content.

kodkod
  • 1,556
  • 4
  • 21
  • 43
  • Point number 2 in this answer is the best option if one doesn't want to do custom painting. Unlike what some other answers say, setting the `Alignment` of the cell is NOT the same as setting its direction to RTL. – Hossein Jun 24 '16 at 10:38
2

Such a property does not exist. You need to set the RightToLeft property of the entire control.

I suspect that you're attempting to mis-use the property to right-justify your text. It's intended to enable support for locales that use right-to-left fonts, not for custom formatting.

If altering formatting is your goal, each DataGridViewCell has a Style property that accepts an instance of the DataGridViewCellStyle class. You can set its Alignment property to "MiddleRight" to align the content of the cell vertically at the middle and horizontally at the right. For more information, see: How to: Format Data in the Windows Forms DataGridView Control.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
1

As simple as that:

DataGridView1.Columns["name of column"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
Yassin
  • 1,376
  • 18
  • 18
1

To do so for the whole column, use

dataGridView.Columns["column name"].DefaultCellStyle.Alignment = DataGridViewAlignment.MiddleRight;

Although I believe the individual cell's style will override this.

Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72
0

I know this is quite an old post, but many times I have found answers to an old post just as helpful to point me to a solution, so I will post my solution anyway.

I did this by handling the EditingControlShowing event of the datagridview. One thing that was throwing me off when solving this was that I was trying to look for a property RightToLeft in a datagridviewcell, however that is a property of Textbox instead.

private void MyDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        TextBox currentCell = e.Control as TextBox;
        if (currentCell != null
            && myDataGridView.CurrentCell.ColumnIndex == NameOfYourColumn.Index) //or compare using column name
        {
            currentCell.RightToLeft = RightToLeft.Yes;
        }
    }
haku
  • 4,105
  • 7
  • 38
  • 63