How can I center the heading in a column on a DataGridView?
Asked Answered
D

2

13

I have a strange problem and it's probably a simple fix, but after much research, I cannot seem to find a solution.

I have a DataGridView on which I'm trying to center the column headings, but the result is a left bias in the centering—almost like an indenting problem. I've seen a few posts on this issue on a site or two, but never a solution. Any thoughts?

Here's the statement I'm currently trying to use:

DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Devora answered 29/12, 2010 at 1:21 Comment(0)
T
23

The code you've posted is on the right track: you need to set the ColumnHeadersDefaultCellStyle property of your DataGridView control.

However, you need to create a new DataGridViewCellStyle class and assign that to the ColumnHeadersDefaultCellStyle property. You can't modify the Alignment property as your code sample shows unless you have assigned a DataGridViewCellStyle class to this property.

So, for example, the following code achieves perfectly centered column headings in a blank project:

Dim dgvColumnHeaderStyle As New DataGridViewCellStyle()
dgvColumnHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

myDataGridView.ColumnHeadersDefaultCellStyle = dgvColumnHeaderStyle

 DataGridView with centered column headings


In the future, you may find it easier to do these types of things from the Designer. If you still need to do it yourself through code, you can check the *.Designer.vb file that is created to see how it was done.


EDIT: I just now noticed the slight offset you're referring to in the columns—it does indeed create a little extra padding to the right of each header. It's not a bug, though. There's a much simpler explanation.

Like a ListView, the DataGridView supports sorting by columns. Therefore, each column header reserves enough space to display the sort glyph (usually an arrow) when calculating center justification.

If you want the column headers to be perfectly centered, you'll need to disable sorting. Set the SortMode property for the column to "NonSortable". This should prevent space from being reserved for the sort glyph whenever the column text is center or right justified.

Tourneur answered 29/12, 2010 at 1:41 Comment(5)
Thank you for your quick response. What I notice though is that you get the same left bias - each of the headings aren't quite centered. I'm reading some info that appears to indicate a bug, that requires each heading to be padded with a few empty spaces, but clearly it varies by the length of the column heading. It looks to be a padding of about 2.5% to be added before the text. Clearly though, that doesn't sound like a solution. I've tried this a few ways and keep getting the same thing. Strange, huh?Devora
BTW, the 2.5% is of the total width of the column... sorry for any confusion generated from the omission.Devora
@Keith: I just now noticed what you're talking about. That's funny, I've never paid any attention to that before. And all of my friends call me OCD—that'll show them! I've updated my answer with some new insight.Tourneur
Just in case anyone else comes along. At least in .NET 4.5, the field name is "DataGridViewColumnSortMode.NotSortable" instead of NonSortable.Coco
at least someone has an explanation for extra padding on right. i thought iw as losing my mindQueeniequeenly
I
2

If you want to center or use any other alignment style of the Column Header text you can use this

dgvResults.Columns("ColumnName").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
Inge answered 4/6, 2016 at 5:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.