Solved datagrid multiline column header

Posted on 2006-04-11
Last Modified: 2009-12-16
  I am developing a windows application. I have formatted the datagrid colums using datagridtextboxcolumn . My column headertext  are long . so how to make columnheader multiline?

Question by:sgs1970
    LVL 96

    Accepted Solution

    Here is a VB.NET custom DataGrid control, converted from a CodeProject C# control, and modified to have HeaderLines instead of pixels:

    ' Source:

    Imports System.ComponentModel
    Imports System.Reflection

    Public Class SizableHeaderDataGrid
      Inherits DataGrid

      Private _headerLines As Single = 1.0

      <Description("Get/Set the factor for the scaling the header row"), _
       Category("Layout"), DefaultValue(1.0)> _
      Public Property HeaderLines() As Single
          Return _headerLines
        End Get
        Set(ByVal Value As Single)
          _headerLines = Value
          OnLayout(New LayoutEventArgs(Me, "HeaderLines"))
        End Set
      End Property  'HeaderLines

      Protected Overloads Overrides Sub OnLayout(ByVal levent As LayoutEventArgs)
      End Sub  'OnLayout

      Protected Overloads Overrides Sub OnPaint(ByVal e As PaintEventArgs)
      End Sub  'OnPaint

      Private Sub SetColumnHeaderSize()
        If Me.HeaderLines > 0 Then
          Dim [myClass] As New DataGrid
          Dim headerFontHeight As FieldInfo = [myClass].GetType.GetField("headerFontHeight", BindingFlags.NonPublic Or BindingFlags.Instance)
          If Not (headerFontHeight Is Nothing) Then
            Dim height As Int32 = Me.Font.Height * Me.HeaderLines
            If Me.HeaderLines > 1 Then
              headerFontHeight.SetValue(Me, height)
            End If
          End If
        End If
      End Sub  'SetColumnHeaderSize

    End Class


    After adding this control to your form, just set the HeaderLines property to >1, and put a vbLf to word wrap the header.


    Author Comment

    I haven't tested as I am with some other assignment. But answers in the past and the profile of Mr.Bob gives me the confidence to accept his answer  without verification

       Thank you,

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now