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

5.1k Views Asked by At

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

5

There are 5 best solutions below

0
On

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.

0
On

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.

1
On

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.

0
On

As simple as that:

DataGridView1.Columns["name of column"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
0
On

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;
        }
    }