DataGrid Headers

Posted on 2004-11-03
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
    LVL 28

    Accepted Solution

    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

    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

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    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…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now