Writing right to left in cell of datagridview

rahul kumar 565 Reputation points
2023-11-07T11:46:37.6266667+00:00
 if (e.ColumnIndex == 1 && 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;
            }


https://www.codeproject.com/Questions/133676/Change-the-RightToLeft-property-of-DataGridView-fo

the above is the code , i am using inside the cellpaint event to right align the text but the problem is it is aligning the text inside the cell after i finish editing the cell .

so i tried using

          //  this.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

but it gives me a run time error as initially my datagridview does not have any data . it is blank on load

I want that it should starts typing from the right to left mode (while being in editing mode) . Is it possible for a datagridview control to have such thing .

thanks in advance .

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,737 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,483 questions
0 comments No comments
{count} votes

Accepted answer
  1. KOZ6.0 4,250 Reputation points
    2023-11-07T15:40:32.6133333+00:00

    Will this work?

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
        var dgv = (DataGridView)sender;
        if (dgv.CurrentCell.ColumnIndex == 1 && dgv.CurrentCell.RowIndex >= 0) {
            var tbox = (TextBox)e.Control;
            tbox.RightToLeft = RightToLeft.Yes;
            tbox.TextAlign = HorizontalAlignment.Right;
        }
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful