How can I right-align text in a DataGridView column? I am writing a .NET WinForms application.
7 Answers
this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

- 19,377
- 11
- 56
- 83
-
13Similarly: 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
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:

- 13,072
- 12
- 67
- 75
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)
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;
}

- 1,245
- 15
- 25
While we are dealing with alignment and configuration it can be useful to reduce repetitive tasks by using (C#) anonymous types. Much easier to read and advanced operations can be edited in one place. Like in this datagridview dg configuration
foreach (var conf in new [] {
new { Col = 0, WidthPercent = 40, Align = DataGridViewContentAlignment.MiddleCenter } ,
new { Col = 1, WidthPercent = 30, Align = DataGridViewContentAlignment.MiddleCenter } ,
new { Col = 2, WidthPercent = 30, Align = DataGridViewContentAlignment.MiddleCenter }
})
{
dg.Columns[conf.Col].Width = (int)(dg.Width * conf.WidthPercent/100);
dg.Columns[conf.Col].DefaultCellStyle.Alignment = conf.Align;
}

- 1,123
- 13
- 9
-
While into datagridview, it is also useful to sometimes transform data and populate a grid with column names defined anonymously. dg.DataSource = MyList.Select(d=>new {Name=d.Key, IP=d.Value.ip, Refreshed = DateTime.Now.ToString("HH:mm:ss") } ).ToList(); – flodis Oct 23 '21 at 13:39
<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

- 9
- 3
-
2Welcome 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
DataGridViewColumn column0 = dataGridViewGroup.Columns[0];
DataGridViewColumn column1 = dataGridViewGroup.Columns[1];
column1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
column1.Width = 120;

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