72

How can I right-align text in a DataGridView column? I am writing a .NET WinForms application.

Cody Gray
  • 222,280
  • 47
  • 466
  • 543
Jayantha Lal Sirisena
  • 20,611
  • 10
  • 65
  • 90

6 Answers6

116
this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; 
MUG4N
  • 18,011
  • 10
  • 52
  • 81
  • 11
    Similarly: go to the Collections property of the datagridview; click on the column; at the top under the Appearance category there will be a DefaultCellStyle field; click '...'; a CellStyle Builder window will pop up with things like Alignment, etc. So, you can set the properties in the form editor without needing to resort to hard coding. – leetNightshade Aug 09 '13 at 18:04
  • 2
    (to leetNightshade...) Setting values in the form editor can be thought of as hard-coding. By setting values in the code (as in the solution given by MUG4N), it is possible to alter the alignment based on data type. E.g. use right-align if a cell contains numeric data or left-align if the cell contains text data. Remember that grids can be populated programmatically at run-time (e.g. if they are not data-bound). It is also possible to read the alignment settings from a configuration source and set them dynamically at run-time. Cheers. – Andrew Jens Mar 24 '15 at 23:53
  • To modify the header cell as well, use `this.dataGridView1.Columns["CustomerName"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;` – Conner Apr 26 '21 at 17:29
7

To set align text in dataGridCell you have two ways:

Set the align for a specific cell or set for each cell of row.

For one column go to Columns->DataGridViewCellStyle

or

For each column go to RowDefaultCellStyle

The control panel is the same as the follow:

enter image description here

daniele3004
  • 10,770
  • 9
  • 55
  • 63
4

I know this is old, but for those surfing this question, the answer by MUG4N will align all columns that use the same defaultcellstyle. I'm not using autogeneratecolumns so that is not acceptable. Instead I used:

e.Column.DefaultCellStyle = new DataGridViewCellStyle(e.Column.DefaultCellStyle);
e.Column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

In this case e is from:

Grd_ColumnAdded(object sender, DataGridViewColumnEventArgs e)  
rgettman
  • 167,281
  • 27
  • 248
  • 326
zDougie
  • 101
  • 2
0

you can edit all the columns at once by using this simple code via Foreach loop

        foreach (DataGridViewColumn item in datagridview1.Columns)
        {
            item.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
        }
0
<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" >
  <DataGridTextColumn.ElementStyle>
    <Style TargetType="{x:Type TextBlock}">
       <Setter Property="HorizontalAlignment" Value="Right" />
    </Style>
  </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
  • 1
    Welcome to Stack Overflow. Code-only answers are discouraged here because they don't explain how it solves the problem. Please edit your answer to explain what the code does and how it answers the question, so that it is useful for other users also as well as the OP. – FluffyKitten Jul 29 '20 at 09:55
-1

DataGridViewColumn column0 = dataGridViewGroup.Columns[0];
DataGridViewColumn column1 = dataGridViewGroup.Columns[1];
column1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
column1.Width = 120;

  • The community encourages adding explanations alongisde code, rather than purely code-based answers (see [here](https://meta.stackoverflow.com/questions/300837/what-comment-should-i-add-to-code-only-answers)). Also, please check out the [formatting help page](https://stackoverflow.com/editing-help) to improve your formatting. – costaparas Feb 16 '21 at 06:38