Solved

VB.Net - DataGridView Center Column Headings

Posted on 2013-01-09
10
568 Views
Last Modified: 2013-01-10
Good Day Experts!

I am having trouble centering the column headings in my DataGridView.  When I have the columns defined prior to loading the DataGrid with data it works fine.  But I can't do that this time.  So, I am trying to do everything programatically. I have the data centered in the columns but I cannot get the column headings centered. Here is what I am trying:

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

Does anything look wrong with my coding?

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
[X]
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
  • 5
  • 4
10 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 38759333
try this one:

dataGridView1.Columns(0).HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomCenter
0
 

Author Comment

by:Jimbo99999
ID: 38759608
Thanks for replying.  However, it did not work. The column headings are still left justified.

Thanks,
jimbo99999
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38759643
Where did you put this code? After binding the grid?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Jimbo99999
ID: 38759967
I have tried it before and after setting the datasource of the DataGrid to the DataTable.  
Everything else works...the readonly, the autosizing and centering of the data in the columns.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38760019
Try following

Dim row As Windows.Forms.DataGridViewRow = dgrdPossDupBills.Rows(-1)
' Loop through header cells
For Each HeaderCell As Windows.Forms.DataGridViewHeaderCell In row.Cells
HeaderCell.Style.Alignment = Windows.Forms.DataGridViewContentAlignment.MiddleCenter
Next
0
 

Author Comment

by:Jimbo99999
ID: 38760708
Thanks for responding

I am receiveing and error indicating the Index was out of range.  Must be non-negative and less than the size of the collection.

jimbo99999
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38761191
Try changing -1 to 0.
0
 

Author Comment

by:Jimbo99999
ID: 38762667
Received error: Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxCell' to type 'System.Windows.Forms.DataGridViewHeaderCell'
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 38763493
Ok. Here is the result of my test


SCR
0
 

Author Closing Comment

by:Jimbo99999
ID: 38764564
Excellent, that did the trick for me.

I will certainly add your assistance on the topic to my reference.

Thanks,
jimbo99999
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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