Solved

DataGridView Header text not centered

Posted on 2009-04-07
5
881 Views
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.

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
Comment
Question by:jppinto
  • 3
  • 2
5 Comments
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
Hi jppinto,
Do you mean the column "URQ" ? Don't you have spaces in the column name ?
0
 
LVL 33

Author Comment

by:jppinto
Comment Utility
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
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
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
 
LVL 33

Author Comment

by:jppinto
Comment Utility
I've tryed your code but with no sucess...the problem still exists!

jppinto
0
 
LVL 33

Accepted Solution

by:
jppinto earned 0 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Custom auto number 6 51
Vb.Net Menu 1 21
DataGridview Currency Formating? 22 37
Copy/Clone an object. 9 10
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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 …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

11 Experts available now in Live!

Get 1:1 Help Now