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

7

88

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

Reinforce answered 8/3, 2011 at 9:0 Comment(0)
D
142
this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; 
Distress answered 8/3, 2011 at 9:3 Comment(3)
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.Stringpiece
(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.Butyrin
To modify the header cell as well, use this.dataGridView1.Columns["CustomerName"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;Costate
T
13

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

Tod answered 4/10, 2016 at 12:50 Comment(0)
S
6

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)  
Sima answered 26/11, 2015 at 20:7 Comment(0)
C
3

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;
        }
Chaetopod answered 3/4, 2019 at 17:17 Comment(0)
P
1
<DataGridTextColumn Header="Quantity" Binding="{Binding Quantity}" >
  <DataGridTextColumn.ElementStyle>
    <Style TargetType="{x:Type TextBlock}">
       <Setter Property="HorizontalAlignment" Value="Right" />
    </Style>
  </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
Pointtopoint answered 29/7, 2020 at 9:28 Comment(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.Singlehearted
D
1

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;
}
Dolce answered 23/10, 2021 at 13:27 Comment(1)
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();Dolce
H
-1

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

Homoousian answered 16/2, 2021 at 6:23 Comment(1)
The community encourages adding explanations alongisde code, rather than purely code-based answers (see here). Also, please check out the formatting help page to improve your formatting.Regenaregency

© 2022 - 2024 — McMap. All rights reserved.