Learn how to a build a cloud-first strategyRegister Now


DataGrid Headers

Posted on 2004-11-03
Medium Priority
Last Modified: 2010-05-18
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?
Question by:curtis591
  • 2
LVL 28

Accepted Solution

iboutchkine earned 1000 total points
ID: 12487672
in your columnstyle put a vbCrLf in your headertext

something like

.HeaderText = "H" & vbCrLf & _
                      "E" & vbCrLf & _
                      "A" & vbCrLf & _
                      "D" & vbCrLf & _
                      "E" & vbCrLf & _
LVL 25

Expert Comment

ID: 12492664
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(0).GridColumnStyles(0).HeaderText = "H" & vbCrLf & _
                              "E" & vbCrLf & _
                              "A" & vbCrLf & _
                              "D" & vbCrLf & _
                              "E" & vbCrLf & _
LVL 25

Assisted Solution

RonaldBiemans earned 1000 total points
ID: 12492712
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)

    End Sub

Featured Post

Industry Leaders: 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 tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline
Suggested Courses

810 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