How to set row direction in DataGridViewCell?

How to set a property RightToLeftfor a specific DataGridViewCell in a DataGridViewColumn?

+3
source share
5 answers

I know this is an old question, but as others have said, DataGridViewCelland DataGridViewColumndo not have properties RightToLeft. However, there are workarounds to this problem:

  • Handle the event CellPaintingand use TextFormatFlags.RightToLeft:

    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 Question CodeProject .)

  • If this is only one specific cell in the DGV, you can try inserting an invisible RTL character (U + 200F) at the beginning of the contents of the cell.

+3
source

. RightToLeft .

, , . , , .

, DataGridViewCell Style, DataGridViewCellStyle class. Alignment "MiddleRight", . . : DataGridView Windows Forms.

+2

,

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

, .

+1

:

DataGridView1.Columns["name of column"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
+1

I know this is a pretty old post, but many times I found the answers to the old post as useful as pointing me to a solution, so I will send my solution anyway.

I did this by handling the EditingControlShowing event in a datagridview. One thing that cast me off when resolving this issue was that I was trying to look up the RightToLeft property in a datagridviewcell, however this is the Textbox property 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;
        }
    }
0
source

Source: https://habr.com/ru/post/1792736/


All Articles