Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 536
Jayantha Lal Sirisena Avatar asked Mar 08 '11 09:03

Jayantha Lal Sirisena


4 Answers

this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; 
like image 178
MUG4N Avatar answered Nov 19 '22 06:11

MUG4N


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

like image 41
daniele3004 Avatar answered Nov 19 '22 07:11

daniele3004


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)  
like image 6
zDougie Avatar answered Nov 19 '22 05:11

zDougie


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;
        }
like image 2
Sayed Muhammad Idrees Avatar answered Nov 19 '22 05:11

Sayed Muhammad Idrees