שתף באמצעות


How to merge two column headers in a datagridview

Question

Monday, October 8, 2012 10:08 AM

I want to merge English Theory and English Practical display like this:

SNo | Student Name | Roll No |  English  |
       |                        |             | Th | Prac |

       |                        |             |      |         |
       |                        |             |      |         |

and same for other Theory and Practical subjects.
Somebody please help.

All replies (13)

Tuesday, October 9, 2012 3:37 AM ✅Answered | 1 vote

Hi auGeek,

Welcome to the MSDN forum.

You need to customer your datagridvew and draw the columns with the CellPainting and Painting events of datagridview.  Following screenshot and code is the sample code from Zhi-Xin Ye, and I have converted to VB.net. You can refer to this to finish your application.

  Private Sub Combine_two_columns_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.DataGridView1.Columns.Add("JanWin", "Win")
        Me.DataGridView1.Columns.Add("JanLoss", "Loss")
        Me.DataGridView1.Columns.Add("FebWin", "Win")
        Me.DataGridView1.Columns.Add("FebLoss", "Loss")
        Me.DataGridView1.Columns.Add("MarWin", "Win")
        Me.DataGridView1.Columns.Add("MarLoss", "Loss")
        Me.DataGridView1.Columns.Add("AprWin", "Win")
        Me.DataGridView1.Columns.Add("AprLoss", "Loss")
        Me.DataGridView1.Rows.Add("1", "2", "3", "2", "2", "2", "4", "2")
        For j As Integer = 0 To Me.DataGridView1.ColumnCount - 1
            Me.DataGridView1.Columns(j).Width = 45
        Next
        Me.DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        Me.DataGridView1.ColumnHeadersHeight = Me.DataGridView1.ColumnHeadersHeight * 2
        Me.DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
    End Sub
    Private Sub DataGridView1_CellPainting(sender As Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
        If e.RowIndex = -1 AndAlso e.ColumnIndex > -1 Then
            Dim r2 As Rectangle = e.CellBounds
            r2.Y += e.CellBounds.Height / 2
            r2.Height = e.CellBounds.Height / 2
            e.PaintBackground(r2, True)
            e.PaintContent(r2)
            e.Handled = True
        End If
    End Sub
    Private Sub DataGridView1_ColumnWidthChanged(sender As Object, e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles DataGridView1.ColumnWidthChanged
        Dim rtHeader As Rectangle = Me.DataGridView1.DisplayRectangle
        rtHeader.Height = Me.DataGridView1.ColumnHeadersHeight / 2
        Me.DataGridView1.Invalidate(rtHeader)
    End Sub
    Private Sub DataGridView1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint
        Dim monthes As String() = {"January", "February", "March", "April"}
        Dim j As Integer = 0
        While j < 8
            Dim r1 As Rectangle = Me.DataGridView1.GetCellDisplayRectangle(j, -1, True)
            Dim w2 As Integer = Me.DataGridView1.GetCellDisplayRectangle(j + 1, -1, True).Width
            r1.X += 1
            r1.Y += 1
            r1.Width = r1.Width + w2 - 2
            r1.Height = r1.Height / 2 - 2
            e.Graphics.FillRectangle(New SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.BackColor), r1)
            Dim format As New StringFormat()
            format.Alignment = StringAlignment.Center
            format.LineAlignment = StringAlignment.Center
            e.Graphics.DrawString(monthes(j \ 2), Me.DataGridView1.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor), r1, format)
            j += 2
        End While
    End Sub
    Private Sub DataGridView1_Scroll(sender As Object, e As System.Windows.Forms.ScrollEventArgs) Handles DataGridView1.Scroll
        Dim rtHeader As Rectangle = Me.DataGridView1.DisplayRectangle
        rtHeader.Height = Me.DataGridView1.ColumnHeadersHeight / 2
        Me.DataGridView1.Invalidate(rtHeader)
    End Sub

Hope this helps.

Mark Liu-lxf [MSFT]
MSDN Community Support | Feedback to us


Monday, October 8, 2012 10:15 AM

Be aware a DataGridView is not a spreadsheet where you can merge many things.

A datagridview is simply a grid which shows data in the way of rows where the cells have gotten a name in the column headers.

Success
Cor


Monday, October 8, 2012 11:44 AM

I performed such a thing earlier, but it was for all the columns, now it is for certain columns only...

So made me confused and i cannot perform the task...


Monday, October 8, 2012 12:19 PM

I performed such a thing earlier, but it was for all the columns, now it is for certain columns only...

So made me confused and i cannot perform the task...

I'm curious at that code, can you show that here?

Success
Cor


Tuesday, October 9, 2012 11:01 AM

The code is been posted by Mark Liu-lxf below


Tuesday, October 9, 2012 11:12 AM

Thankyou, I got the desired output.


Tuesday, October 9, 2012 11:22 AM

Thanks for the help.


Friday, October 23, 2015 10:39 AM

Hello

i know this is an old post but i tried the above code and I do get a higher first row of the header but there is no January etc at the top just a grey backgound and below it the win , loss etc

I am using VS2010

best regards

Patrick


Wednesday, June 1, 2016 9:29 AM

how if you use the data using SQL Server Database ??

because I find it difficult to amend the columns and their data using a database that SQL Sever


Friday, August 26, 2016 10:36 AM

do you have c# code version?

and i want to know how can i do it as below picture

thanks!


Friday, August 26, 2016 10:45 AM

Paipay,

Don't hack other persons threads. Create a new question. 

Mark Liu is for sure not watching this thread anymore. 

Success
Cor


Friday, August 26, 2016 10:54 AM

I had create another question

https://social.msdn.microsoft.com/Forums/windows/en-US/53558077-7d5c-433a-8303-c7f2b318d2ac/how-can-i-design-or-draw-the-header-of-winforms-datagridview-as-below?forum=winformsdatacontrols

but no one reply on it so I found this question in familiar 

than I thought it will be fast to solve my question


Friday, August 26, 2016 11:36 AM

I replied it there but you won't be lucky with that. 

Success
Cor