954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Change Data Grid Column header alignment to center?

How could I change the column header of the Data Grid to center or right but not left as default?
[Pls pay attention, We are talking 'bout Data Grid here, recently I always receive answers for Data Grid View instead :( ]
I just want to re-align the header only, not the whole column. There's another topic discussed bout this already but it's not working in my case. Could somebody help me? Here is my code:

Public Class Form1

    Private Sub DataGrid1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGrid1.Paint
        Dim row As Integer
        Dim p As Point
        Dim yChange As Integer
        Dim y As Integer
        Dim RowText As String
        Dim hti As DataGrid.HitTestInfo
        p = New Point(DataGrid1.GetCellBounds(0, 0).X + 4, DataGrid1.GetCellBounds(0, 0).Y + 4)
        Static FirstLoad As Boolean = True
        Try
            hti = DataGrid1.HitTest(p)
            row = hti.Row
            yChange = DataGrid1.GetCellBounds(row, 0).Height + 1
            y = DataGrid1.GetCellBounds(row, 0).Top + 2
            While (y <= DataGrid1.Height - yChange And row < DataSet11.Tables("nhanvien").Rows.Count)
                RowText = (row + 1).ToString
                e.Graphics.DrawString(RowText, DataGrid1.Font, New SolidBrush(Color.Black), 12, y)
                y += yChange
                row += 1
            End While
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataSet11.Clear()
        OleDbDataAdapter1.Fill(DataSet11)
        DataGrid1.DataSource = DataSet11.Tables("nhanvien")
        DataGrid1.TableStyles.Clear()
        Dim ts As DataGridTableStyle
        ts = New DataGridTableStyle
        ts.ColumnHeadersVisible = True
        ts.MappingName = "nhanvien"
        For Each column As DataColumn In DataSet11.Tables("nhanvien").Columns
            Dim columnStyle As New DataGridTextBoxColumn
            columnStyle.MappingName = column.ColumnName
            columnStyle.HeaderText = column.ColumnName
            columnStyle.HeaderText = column.ColumnName
            columnStyle.Alignment = HorizontalAlignment.Center
            ts.GridColumnStyles.Add(columnStyle)
            DataGrid1.TableStyles.Add(ts)
            DataGrid1.TableStyles(0).RowHeaderWidth = 50
        Next
        DataGrid1.TableStyles.Add(ts)
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            OleDbDataAdapter1.Update(DataSet11)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
Eternal Newbie
Newbie Poster
20 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

No one's helping, yet??? Oh, I'm needing this so bad, could smone help me, pls??? :(

Eternal Newbie
Newbie Poster
20 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Okay, I got it. May be I'll set this to solved, noone seeing this anyway!

Eternal Newbie
Newbie Poster
20 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: