How can I right-align text in a DataGridView colum

2019-03-10 12:08发布

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

4条回答
爷的心禁止访问
2楼-- · 2019-03-10 12:47

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 acceptible. Instead I used:

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

e in this case is from:

Grd_ColumnAdded(object sender, DataGridViewColumnEventArgs e)  
查看更多
爷的心禁止访问
3楼-- · 2019-03-10 12:47

i have same problem and i finally found the answer there is an option in properties in datagridview , it name is "Right to left" just change to "yes"

it works!

查看更多
甜甜的少女心
4楼-- · 2019-03-10 12:52

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

查看更多
做个烂人
5楼-- · 2019-03-10 13:00
this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; 
查看更多
登录 后发表回答