Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


DataGridView Header text not centered

Posted on 2009-04-07
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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