Alignment issue in datagridview

rahul kumar 605 Reputation points
2023-11-08T09:53:46.4366667+00:00

Hi ,

I am struggling to set the alignment of the text inside the datagridview column when it is done . I am using

  public CustomDataGridView()
        {
            InitializeComponent();
           // this.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
        }

but found out that since my customdatagridview doesn't have columns initially and i add the columns only on form , the above code throws error .

so just for checking , i add the code in form load

  public Form1()
        {
            InitializeComponent();
           
            this.customDataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
        }

but it gave me a weird result . as although i instruct the column1(in oneditingcontrolshowing) to start typing from right to left (see below code ) , the system weirdly starts typing from the left to right only and that too only for column1

 protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox textBox = e.Control as TextBox; // var tbox = (TextBox)e.Control;
            if (textBox != null)
            {
                disableSelectWindow = new DisableSelectWindow(textBox);
                textBox.ContextMenuStrip = new ContextMenuStrip(); // disable contect menu 
            }
            // except starting column ie.  0  , all columns must type from right to left 
            if (this.CurrentCell.ColumnIndex > 0 && this.CurrentCell.RowIndex >= 0)
            {                
                textBox.RightToLeft = RightToLeft.Yes;
                textBox.TextAlign = HorizontalAlignment.Right;
            }
            base.OnEditingControlShowing(e);
        }

Now the reason for not defining the column names and column count in the declaration of customdatagridview is that , this custom datagridview would be used in multiple Forms and each time it would have different number of columns but one thing is sure that except column0 . every column will starts typing from right to left and after that must aligned to right side only . Below is the link for my github project pls correct my code behaviour .

https://github.com/RahulKumarcool/Rahulkumar

Developer technologies Windows Forms
Developer technologies C#
{count} votes

Accepted answer
  1. KOZ6.0 6,655 Reputation points
    2023-11-09T01:27:38.1466667+00:00

    The RightToLeft property is used when inputting characters that are written from right to left, such as Arabic or Hebrew.

    https://learn.microsoft.com/en-us/answers/questions/1419019/writing-right-to-left-in-cell-of-datagridview

    I answered this because you were specifying the format with TextFormatFlags.RightToLeft, but it is not necessary if you just want to right-align it.

    Please specify right alignment (TopRight, MiddleRight, BottomRight) using the DefaultCellStyle.Alignment property.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.