Results 1 to 5 of 5

Thread: [RESOLVED] Centering Text in Column Headers on datagridview

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

    Resolved [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.

    Name:  grid.jpg
Views: 12023
Size:  32.6 KB

    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.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    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:
    Name:  dgv.png
Views: 12957
Size:  10.1 KB
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    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

  4. #4
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    236

    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
  •  



Click Here to Expand Forum to Full Width