[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1015
  • Last Modified:

DataGridView Header text not centered

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.

jppinto
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

DataGridView.JPG
0
jppinto
Asked:
jppinto
  • 3
  • 2
1 Solution
 
Jorge PaulinoIT Pro/DeveloperCommented:
Hi jppinto,
Do you mean the column "URQ" ? Don't you have spaces in the column name ?
0
 
jppintoAuthor Commented:
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.
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Strange ... Try this snippet after your code
        For Each col As DataGridViewColumn In Me.DataGridView1.Columns
            col.HeaderText = col.HeaderText.ToString.Trim
        Next

Open in new window

0
 
jppintoAuthor Commented:
I've tryed your code but with no sucess...the problem still exists!

jppinto
0
 
jppintoAuthor Commented:
Solved!

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

jppinto
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now