Results 1 to 8 of 8

Thread: Vertically up Headers

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Posts
    220

    Vertically up Headers

    Hi guys, I am trying to design a form with headers that read vertically up. This is to save space so I can fit many columns. I tried datagrid but am not sure how to achieve this. I use WPF and Vb.net. Please suggest any other better solution eg importing an excel or doc table, if that is possible. I have never tried that before. I am a little stuck. Thanks if you can help.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Vertically up Headers

    I've been playing around with this for a while with some fairly unspectacular results so the best I can offer at the moment is vertically down headers. Consider this a work in progress!

    vb.net Code:
    1. ' in the form which has your dgv
    2.  
    3.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    4.         Column1.HeaderCell = New VerticalColumnHeader
    5. ' etc.
    6.     End Sub
    7.  
    8. ' and the class
    9.  
    10. Class VerticalColumnHeader
    11.     Inherits DataGridViewColumnHeaderCell
    12.  
    13.     Protected Overrides Sub Paint(graphics As System.Drawing.Graphics, clipBounds As System.Drawing.Rectangle, cellBounds As System.Drawing.Rectangle, rowIndex As Integer, dataGridViewElementState As System.Windows.Forms.DataGridViewElementStates, value As Object, formattedValue As Object, errorText As String, cellStyle As System.Windows.Forms.DataGridViewCellStyle, advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, paintParts As System.Windows.Forms.DataGridViewPaintParts)
    14.         MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, "", "", errorText, cellStyle, advancedBorderStyle, paintParts)
    15.         Dim f As New StringFormat
    16.         f.FormatFlags = StringFormatFlags.DirectionVertical
    17.         graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    18.         graphics.DrawString(value.ToString, cellStyle.Font, New Drawing.SolidBrush(cellStyle.ForeColor), 50, 20, f)
    19.     End Sub
    20.  
    21. End Class

    EDIT: Oh, nearly forgot, in the DGV you'll need to set ColumnHeadersHeightSizeMode to EnableResizing so that you can increase ColumnHeadersHeight to a suitable size.
    Last edited by dunfiddlin; Jun 25th, 2013 at 08:55 PM.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Vertically up Headers

    As you're overriding the Paint event it's just 1 small step to rotate the text.
    Have a look at the Graphics.RotateTransform method

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Vertically up Headers

    Here's an example of rotating text:

    http://code.msdn.microsoft.com/VerticalLabel-660dbce3

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Vertically up Headers

    Quote Originally Posted by .paul. View Post
    As you're overriding the Paint event it's just 1 small step to rotate the text.
    Have a look at the Graphics.RotateTransform method

    Ta muchly but I did try that (and will do so again) but there appear to be complications associated with this cell that I didn't get time to look into (it's 3 in the morning ... why am I even still up?)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Posts
    220

    Re: Vertically up Headers

    Quote Originally Posted by dunfiddlin View Post
    Ta muchly but I did try that (and will do so again) but there appear to be complications associated with this cell that I didn't get time to look into (it's 3 in the morning ... why am I even still up?)
    Since I am using WPF I will try the RotateTransform approach. It looks more relevant and to the point. Anyway, thanks for all your replies guys, appreciated.

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Vertically up Headers

    I really got to learn not to to try topology at 2.00 in the morning! My math was the problem (in this case not remembering which way was up after a rotation!) So here is the working version.

    vb.net Code:
    1. Class VerticalColumnHeader
    2.     Inherits DataGridViewColumnHeaderCell
    3.  
    4.     Protected Overrides Sub Paint(graphics As System.Drawing.Graphics, clipBounds As System.Drawing.Rectangle, cellBounds As System.Drawing.Rectangle, rowIndex As Integer, dataGridViewElementState As System.Windows.Forms.DataGridViewElementStates, value As Object, formattedValue As Object, errorText As String, cellStyle As System.Windows.Forms.DataGridViewCellStyle, advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, paintParts As System.Windows.Forms.DataGridViewPaintParts)
    5.         MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, "", "", errorText, cellStyle, advancedBorderStyle, paintParts)
    6.         graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    7.         graphics.TranslateTransform(0, cellBounds.Height)
    8.         graphics.RotateTransform(270)
    9.         graphics.DrawString(value.ToString, cellStyle.Font, New Drawing.SolidBrush(cellStyle.ForeColor), 20, 50)
    10.         graphics.ResetTransform()
    11.     End Sub
    12.  
    13. End Class
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Vertically up Headers

    Since this is a WPF question, I am going to move this thread to the WPF Forum.

    Let me know if you have any questions.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width