Solved

Vertical Text in Datagridview Header

Posted on 2013-02-02
10
4,555 Views
Last Modified: 2015-03-18
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.
0
Comment
Question by:G Scott
[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
  • 6
  • 3
10 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38848194
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
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 500 total points
ID: 38848199
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
 
LVL 1

Author Closing Comment

by:G Scott
ID: 38848614
Thanks jtoutou, that is perfect.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Author Comment

by:G Scott
ID: 38848737
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38848825
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38848854
It is working for me without reversing the cells values ,just the column headers..
Tell me if your problem remains.
0
 
LVL 1

Author Comment

by:G Scott
ID: 38851074
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38851185
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 38851447
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
 

Expert Comment

by:SmashAndGrab
ID: 40673111
Hi all,

Does anyone have this solution in c#?

Thanks

Stuart
0

Featured Post

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!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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