-
May 15th, 2014, 03:06 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Centering Text in Column Headers on datagridview
I can't seem to get my text centered in column headers on a datagridview for some reason. The text in the cells is centered ... but not in the column headers. I'm using VB 2012 with the default datagridview.
This is the code used to create that grid: (I used a datatable because I've got a number of grids in my program and they almost all use datatables for the source.)
Code:
Dim table As DataTable = New DataTable("TABLE")
' Declare variables for DataColumn and DataRow objects.
Dim column As DataColumn
column = New DataColumn()
column.DataType = Type.GetType("System.String")
column.ColumnName = "Col 1"
' Add the column to the table.
table.Columns.Add(column)
column = New DataColumn()
column.DataType = Type.GetType("System.String")
column.ColumnName = "Col 2"
' Add the column to the table.
table.Columns.Add(column)
table.Rows.Add("Row 1", "Row 1")
table.Rows.Add("Row 2", "Row 2")
table.Rows.Add("Row 3", "Row 3")
grdSample.DataSource = table
With grdSample
' Set property values appropriate for read-only display and
' limited interactivity.
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.AllowUserToOrderColumns = True
.ReadOnly = True
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None
.AllowUserToResizeColumns = False
.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
.AllowUserToResizeRows = False
.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing
' Special Appearance
.RowHeadersVisible = False
.Font = New Font("CourierNew", 10)
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
.Columns("Col 1").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns("Col 1").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns("Col 2").HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns("Col 2").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
End With
grdSample.Rows(0).Selected = True
As far as I can find the commands listed above should center the text in the column headers ... but they don't. If I change the alignment to justify right or left the text will do that ... but it won't center. What is it I'm missing here or is this a bug?
[EDIT] I should also mention that I have tried manually sizing the columns but I still get the text off center.
TIA
Ken
Last edited by SparrowHawk7; May 15th, 2014 at 03:11 PM.
-
May 15th, 2014, 03:44 PM
#2
Re: Centering Text in Column Headers on datagridview
I literally docked a DataGridView to my form, named it grdSample, and added the code you provided in my form's load event. My headers were centered:
-
May 15th, 2014, 03:52 PM
#3
Re: Centering Text in Column Headers on datagridview
It could be at the moment you are doing it. Change ("Col 1") for (0) and test it like that
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
May 15th, 2014, 03:56 PM
#4
Re: Centering Text in Column Headers on datagridview
What happens if after you have it centered, you make the column wider? it could be that because of the space used for the "sort" arrow, it appears to be off center. Change the columns sortable property to "Not Sortable"
Last edited by kaliman79912; May 15th, 2014 at 03:59 PM.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
May 15th, 2014, 04:53 PM
#5
Thread Starter
Addicted Member
Re: Centering Text in Column Headers on datagridview
Yep ... that was it .. the sorting icon. Thanks a million. I might try fiddling around and try to adjust the spaces to allow for the sorting arrow since I would like the user to have that available ... but it's probably more trouble than that feature is worth.
Thanks again!
Ken
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|