Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the direction of a string in a DataGridViewCell?

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

like image 480
mbmsit Avatar asked Jan 21 '23 11:01

mbmsit


2 Answers

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.

like image 133
kodkod Avatar answered Jan 31 '23 01:01

kodkod


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.

like image 42
Cody Gray Avatar answered Jan 30 '23 23:01

Cody Gray