Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi all,
How can I change the RightToLeft property for the column of a DataGridView and not the whole DataGridView.

i.e
I want my DataGridView to contain two columns, one RightToLef and the other one LeftToRight.

Solution found:
Handle the DataGridView.CellPainting event and add the following code, which simply draws the cell and align it to the right and rtl flags.

C#
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;
       }
   }



Thanks,

Gabi.
Posted
Updated 5-Dec-10 2:44am
v2
Comments
Manfred Rudolf Bihy 5-Dec-10 8:47am    
Hi Gabi, I moved your solution to your question. Thanks for sharing!

 
Share this answer
 
v2
You can't - you can only change the RightToLeft property for a control: DataGridViewColumn is not a control, so it does not have a RightToLeft property.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 5-Dec-10 7:25am    
My vote of 5: Answer is technically correct as you can't control it directly using DataGridViewTextColumn.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900