Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Vertical Text in Datagridview Header

Posted on 2013-02-02
10
Medium Priority
?
4,900 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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

704 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