DataGridView Header text not centered

Posted on 2009-04-07
Last Modified: 2012-05-06
I've a DataGridView where the header texts aren't correctly centered, as you can see by the picture. I've the following code for my DataGridView. Why does this happens?

Thanks for your help.

Me.grdMissingParts.RowHeadersWidth = 25

Me.grdMissingParts.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

Me.grdMissingParts.Columns("Part Number").Width = 150

Me.grdMissingParts.Columns("Part Number").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

Me.grdMissingParts.Columns("Descrição").Width = 250

Me.grdMissingParts.Columns("Descrição").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

Me.grdMissingParts.Columns("URQ").Width = 60

Me.grdMissingParts.Columns("URQ").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

Me.grdMissingParts.Columns("Paragem").Width = 60

Me.grdMissingParts.Columns("Paragem").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

Open in new window

Question by:jppinto
  • 3
  • 2
LVL 48

Expert Comment

ID: 24091870
Hi jppinto,
Do you mean the column "URQ" ? Don't you have spaces in the column name ?
LVL 33

Author Comment

ID: 24091917
No, I don't have spaces in the column name. The problem is with the first 3 columns name as you can see by the picture, they are not well centered.
LVL 48

Expert Comment

ID: 24092043
Strange ... Try this snippet after your code
        For Each col As DataGridViewColumn In Me.DataGridView1.Columns

            col.HeaderText = col.HeaderText.ToString.Trim


Open in new window

LVL 33

Author Comment

ID: 24102590
I've tryed your code but with no sucess...the problem still exists!

LVL 33

Accepted Solution

jppinto earned 0 total points
ID: 24102753

The column header cells reserves space for the sort glyph when calculating center justification. I had to disable sorting on the columns so that the alignment looks centered. Like this:

Me.grdMissingParts.Columns("Part Number").SortMode = DataGridViewColumnSortMode.NotSortable


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now