How can I right-align text in a DataGridView column?

.NetWinformsDatagridviewDatagridviewcolumn

.Net Problem Overview


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

.Net Solutions


Solution 1 - .Net

this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

Solution 2 - .Net

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

Solution 3 - .Net

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)  

Solution 4 - .Net

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

Solution 5 - .Net

<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" >
  <DataGridTextColumn.ElementStyle>
    <Style TargetType="{x:Type TextBlock}">
       <Setter Property="HorizontalAlignment" Value="Right" />
    </Style>
  </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

Solution 6 - .Net

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

Solution 7 - .Net

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJayantha Lal SirisenaView Question on Stackoverflow
Solution 1 - .NetMUG4NView Answer on Stackoverflow
Solution 2 - .Netdaniele3004View Answer on Stackoverflow
Solution 3 - .NetzDougieView Answer on Stackoverflow
Solution 4 - .NetSayed Muhammad IdreesView Answer on Stackoverflow
Solution 5 - .NetMansurPatelView Answer on Stackoverflow
Solution 6 - .NetflodisView Answer on Stackoverflow
Solution 7 - .NetAbdul LatheefView Answer on Stackoverflow