DataGridView with RTL Columns

I had an interesting customer scenario, which is common among developers that deal with more than language. Basically you have an English WinForm application that uses the DataGridView , but you have the Names field in Arabic. So, you want to keep the DataGridView in English , but change one column only to right-to-left.

I have the following easy steps to accomplish this. First you need to define two things:

You’ll use the Unicode RightToLeft Embedding character to help you display the tooltips properly.

   private char rle_char = (char)0x202B;// RLE embedding Unicode control character

To make things simple, I’ll define the Column ID that needs to be RTL.

   private int RTLColumnID = 1; // This sample assumes that Column "1" will be RTL

First, handle the DataGridView.CellPainting event and add the following code, which simply draws the cell and align it to the right and rtl flags.

   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;

       }

  }

Second, handle the DataGridView.EditingControlShowing event. You need to set RightToLeft=Yes to this edit control.

private void RTLColumnsDGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

  {

    if (e.Control != null)

    {

       DataGridView DGridView = sender as DataGridView;

       if (DGridView.CurrentCell.ColumnIndex == RTLColumnID)

          e.Control.RightToLeft = RightToLeft.Yes;

     }

   }

Third, handle the DataGridView.CellToolTipTextNeeded event. You need to add the RLE Unicode control character to make sure the

private void RTLColumnsDGV_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)

  {

    if (e.ColumnIndex == RTLColumnID && e.RowIndex >= 0)

      if (e.ToolTipText != String.Empty)

         e.ToolTipText = rle_char + e.ToolTipText;

  }

I recommend that you don’t set the cell alignment for these columns, leave it as “notset” so that it isn’t confusing.

The above steps will fix the cells but what about the title? Well, in the Form.Load you can set the title to Right align and add the RLE_Char to the beginning to display it properly. Thisis the code for your reference:

  DGridView.Columns[RTLColumnID].HeaderText = rtlchar+DGridView.Columns[RTLColumnID].HeaderText;

  if (DGridView.Columns[RTLColumnID].HeaderCell.ToolTipText != String.Empty)

    DGridView.Columns[RTLColumnID].HeaderCell.ToolTipText = rtlchar + DGridView.Columns[RTLColumnID].HeaderCell.ToolTipText;

  DGridView.Columns[RTLColumnID].HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopRight;

The same rational applies if you have more than one column too. I hope this helps :)