DataGrid Headers

Is there a way to to rotate the text in the header of a .NET datagrid so that the column captions would run vertically rather than horizontally?
LVL 6
curtis591Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

iboutchkineCommented:
in your columnstyle put a vbCrLf in your headertext

something like

.HeaderText = "H" & vbCrLf & _
                      "E" & vbCrLf & _
                      "A" & vbCrLf & _
                      "D" & vbCrLf & _
                      "E" & vbCrLf & _
                      "R"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RonaldBiemansCommented:
Hi Curtis591,

For Iboutchkine suggestion to work you also have to increase the height of the gridcolumn.
Since there is no gridcolumnstyle.height you have to fake that by using a DataGrid1.HeaderFont that is realy big

something like

Dim f As New System.Drawing.Font("Arial", 50.0F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, System.Byte))
        DataGrid1.HeaderFont = f
        Dim grdTableStyle1 As New DataGridTableStyle
        grdTableStyle1.MappingName = "YourTableName"
        DataGrid1.TableStyles.Add(grdTableStyle1)

        DataGrid1.TableStyles(0).GridColumnStyles(0).HeaderText = "H" & vbCrLf & _
                              "E" & vbCrLf & _
                              "A" & vbCrLf & _
                              "D" & vbCrLf & _
                              "E" & vbCrLf & _
                              "R"
0
RonaldBiemansCommented:
You could also try using the paint event of the grid to draw a vertical text in the columns yourself, something like
(remember here you have to increase the height aswell like show in my previous comment)

Private Sub DataGrid1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGrid1.Paint


        Dim gg As New StringFormat
        gg.FormatFlags = StringFormatFlags.DirectionVertical
        For ff As Integer = 0 To dt.Columns.Count - 1
            Dim x As Rectangle = DataGrid1.GetCellBounds(0, ff)
            x.Y = 22
            x.Height += 22

            e.Graphics.FillRectangle(New SolidBrush(DataGrid1.HeaderBackColor), x)
            e.Graphics.DrawString(CType(DataGrid1.DataSource, DataTable).Columns(ff).ToString(), DataGrid1.Font, New SolidBrush(Color.Black), x.X + 2, x.Y + 1, gg)

        Next
    End Sub
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.