Writing right to left in cell of datagridview

rahul kumar 565 Reputation points
 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;


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

    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