Vertical Text in Datagridview Header

I have a datagridview that I need to put the header text vertically.  I have found a few things on here and using Google, but nothing seems to work.  This is the latest thing I have tried, but it doesn't even seem to fire off:

 Private Sub dataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs)
        If e.RowIndex = -1 AndAlso e.ColumnIndex = 0 Then
            e.PaintBackground(e.CellBounds, True)
            e.Graphics.TranslateTransform(e.CellBounds.Left, e.CellBounds.Bottom)
            e.Graphics.RotateTransform(270)
            e.Graphics.DrawString(e.FormattedValue.ToString(), e.CellStyle.Font, Brushes.Black, 5, 5)
            e.Graphics.ResetTransform()
            e.Handled = True
        End If
    End Sub

Can this even be done?  Why is vertical text anywhere so difficult in VB.NET?  Thanks for any help you can give me on this.
LVL 1
G ScottAsked:
Who is Participating?
 
John (Yiannis) ToutountzoglouConnect With a Mentor Instructor Multiengine PilotCommented:
or ?
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If DataGridView1.Columns.Count > 0 Then
            DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
            DataGridView1.ColumnHeadersHeight = 100
        Else
            DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
        End If
        DataGridView1.Columns(0).Width = 20
        
    End Sub

    Private Sub dataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
        If e.RowIndex = -1 AndAlso e.ColumnIndex = 0 Then
            e.PaintBackground(e.CellBounds, True)
            e.Graphics.TranslateTransform(e.CellBounds.Left, e.CellBounds.Bottom)
            e.Graphics.RotateTransform(270)
            e.Graphics.DrawString(e.FormattedValue, e.CellStyle.Font, Brushes.Black, 5, 5)
            e.Graphics.ResetTransform()
            e.Handled = True
        End If
    End Sub

Open in new window

You can Define which column you want a vertical text using e.ColumnIndex the index of your Desired Column
Also if you need the text in opossite vertical direction (Up to Down) change the code to :
 e.Graphics.TranslateTransform(e.CellBounds.Right, e.CellBounds.Top)
            e.Graphics.RotateTransform(90)

Open in new window


P.S Check if DatagridView1.CellPainting Event is selected ...
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
hi ...try this ...your are so close ...
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If DataGridView1.Columns.Count > 0 Then
            DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
            DataGridView1.ColumnHeadersHeight = 100
        Else
            DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
        End If
        For i As Integer = 0 To DataGridView1.Columns.Count - 1
            Dim column As DataGridViewColumn = New DataGridViewCheckBoxColumn()
            DataGridView1.Columns.Insert(DataGridView1.Columns(0).Index + 1, column)
            column.HeaderText = DataGridView1.Columns(0).HeaderText.ToString
            column.Width = 20
        Next
        
    End Sub

    Private Sub dataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
        If e.RowIndex = -1 AndAlso e.ColumnIndex = 1 Then
            e.PaintBackground(e.CellBounds, True)
            e.Graphics.TranslateTransform(e.CellBounds.Left, e.CellBounds.Bottom)
            e.Graphics.RotateTransform(270)
            e.Graphics.DrawString(e.FormattedValue, e.CellStyle.Font, Brushes.Black, 5, 5)
            e.Graphics.ResetTransform()
            e.Handled = True
        End If
    End Sub

Open in new window

hope it helps!!!!
0
 
G ScottAuthor Commented:
Thanks jtoutou, that is perfect.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
G ScottAuthor Commented:
jtoutou, if you are still around, can you tell me what would cause this?
One Column
Here is my entire code:
   Private Sub dataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
        If e.RowIndex = -1 AndAlso e.ColumnIndex = 3 Or e.ColumnIndex = 2 Or e.ColumnIndex = 1 Or e.ColumnIndex = 4 Or e.ColumnIndex = 5 Or e.ColumnIndex = 6 Or e.ColumnIndex = 7 Or e.ColumnIndex = 8 Or e.ColumnIndex = 9 Or e.ColumnIndex = 10 Or e.ColumnIndex = 11 Or e.ColumnIndex = 12 Or e.ColumnIndex = 13 Or e.ColumnIndex = 14 Then
            e.PaintBackground(e.CellBounds, True)
            e.Graphics.TranslateTransform(e.CellBounds.Left, e.CellBounds.Bottom)
            e.Graphics.RotateTransform(270)
            e.Graphics.DrawString(e.FormattedValue, e.CellStyle.Font, Brushes.Black, 5, 5)
            e.Graphics.ResetTransform()
            e.Handled = True
        End If
    End Sub



    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim cn As SqlConnection = New SqlConnection("Data Source=M2036844\SQLExpress;Initial Catalog=ForkliftCheckSheet;Persist Security Info=True;User ID=sa;Password=GoBlue2012!")
        cn.Open()
        Try
            'Add 4 columns and column names to the datatable
            Dim myTable As DataTable = New DataTable("details")
            myTable.Columns.Add(New DataColumn("Hi-Lo Number"))
            myTable.Columns.Add(New DataColumn("Tires"))
            myTable.Columns.Add(New DataColumn("Hyd Cont"))
            myTable.Columns.Add(New DataColumn("Hyd Leak"))
            myTable.Columns.Add(New DataColumn("Steering"))
            myTable.Columns.Add(New DataColumn("Brakes"))
            myTable.Columns.Add(New DataColumn("Horn"))
            myTable.Columns.Add(New DataColumn("Backup Alarm"))
            myTable.Columns.Add(New DataColumn("Battery Conn"))
            myTable.Columns.Add(New DataColumn("Headlights"))
            myTable.Columns.Add(New DataColumn("Warning Light"))
            myTable.Columns.Add(New DataColumn("Dash Gauges"))
            myTable.Columns.Add(New DataColumn("Grnd Chain"))
            myTable.Columns.Add(New DataColumn("Seat Belt"))
            myTable.Columns.Add(New DataColumn("Signs"))
            myTable.Columns.Add(New DataColumn("Operator"))
            myTable.Columns.Add(New DataColumn("Completed On"))
            Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM tblCurrent ORDER BY forklift;", cn)
            Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.Default)
            Dim myRow As DataRow
            Dim ds As DataSet
            While dr.Read()
                myRow = myTable.NewRow
                myRow.Item(0) = dr(0)   'We add the 1st value of the row to our 1st column of the datatable
                myRow.Item(1) = dr(1)   'We add the 4th value of the row to our 2nd column of the datatable
                myRow.Item(2) = dr(2)   'We add the 5th value of the row to our 3rd column of the datatable
                myRow.Item(3) = dr(3)
                myRow.Item(4) = dr(4)   'We add the 1st value of the row to our 1st column of the datatable
                myRow.Item(5) = dr(5)   'We add the 4th value of the row to our 2nd column of the datatable
                myRow.Item(6) = dr(6)   'We add the 5th value of the row to our 3rd column of the datatable
                myRow.Item(7) = dr(7)
                myRow.Item(8) = dr(8)   'We add the 1st value of the row to our 1st column of the datatable
                myRow.Item(9) = dr(9)   'We add the 4th value of the row to our 2nd column of the datatable
                myRow.Item(10) = dr(10)   'We add the 5th value of the row to our 3rd column of the datatable
                myRow.Item(11) = dr(11)
                myRow.Item(12) = dr(12)   'We add the 1st value of the row to our 1st column of the datatable
                myRow.Item(13) = dr(13)   'We add the 4th value of the row to our 2nd column of the datatable
                myRow.Item(14) = dr(14)   'We add 
                myRow.Item(15) = dr(15)
                myRow.Item(16) = dr(16)
                myTable.Rows.Add(myRow)
            End While
            dr.Close()
            Dim myData4 As DataTable = myTable
            ds = New DataSet()
            ds.Tables.Add(myData4)
            DataGridView1.DataSource = myData4
        Catch ex As Exception
            MsgBox(ex.ToString)  'Show error when something goes wrong
        End Try
        cn.Close()
        If DataGridView1.Columns.Count > 0 Then
            DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
            DataGridView1.ColumnHeadersHeight = 150
        Else
            DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
        End If
        DataGridView1.Columns(0).Width = 300
        DataGridView1.Columns(1).Width = 50
        DataGridView1.Columns(2).Width = 50
        DataGridView1.Columns(3).Width = 50
        DataGridView1.Columns(4).Width = 50
        DataGridView1.Columns(5).Width = 50
        DataGridView1.Columns(6).Width = 50
        DataGridView1.Columns(7).Width = 50
        DataGridView1.Columns(8).Width = 50
        DataGridView1.Columns(9).Width = 50
        DataGridView1.Columns(10).Width = 50
        DataGridView1.Columns(11).Width = 50
        DataGridView1.Columns(12).Width = 50
        DataGridView1.Columns(13).Width = 50
        DataGridView1.Columns(14).Width = 50
        DataGridView1.Columns(16).Width = 300
        DataGridView1.Columns(17).Width = 300
        'sizerows()
    End Sub

Open in new window

0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Sorry ...it is sunday...:) Try this
 
Private Sub dataGridView1_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
        If e.RowIndex = -1 then 
Dim i as integer =0
For i=3 to 14
      if  e.ColumnIndex = i
            e.PaintBackground(e.CellBounds, True)
            e.Graphics.TranslateTransform(e.CellBounds.Left, e.CellBounds.Bottom)
            e.Graphics.RotateTransform(270)
            e.Graphics.DrawString(e.FormattedValue, e.CellStyle.Font, Brushes.Black, 5, 5)
            e.Graphics.ResetTransform()
            e.Handled = True
        End If
Next
     End if 
    End Sub

Open in new window

0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
It is working for me without reversing the cells values ,just the column headers..
Tell me if your problem remains.
0
 
G ScottAuthor Commented:
jtoutou, that is brilliant.  Thanks!  It's working perfectly.   One last question for fun. In that above code, is there a way to add a backcolor to that code to color them as well within that block of code?  I don't need this in any way, just more curious than anything.  Thanks, again.
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
I do not rememeber well and i do not have time to try now...try this
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue
dataGridView1.EnableHeadersVisualStyles = False

Open in new window

I promise you i will try it later...


John (Yiannis)
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Yeah,,,I think this is it. In ur form load event before your End Sun inert these two Lines..
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue
dataGridView1.EnableHeadersVisualStyles = False

Open in new window


This means that all your Columns Header will change in your desire colour...

to set Back Color there is also another way in the CellPainting event with these 2 lines
                 Dim backColorBrush As New SolidBrush(Color.Blue)
                e.Graphics.FillRectangle(backColorBrush, e.CellBounds)

Open in new window

Note.....I do not Like Graphics...  :)
0
 
SmashAndGrabCommented:
Hi all,

Does anyone have this solution in c#?

Thanks

Stuart
0
All Courses

From novice to tech pro — start learning today.