Click here to Skip to main content
15,877,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to merge two column headers and give it a new header for Windows Forms

Like this:

----------------------------------------
SNO | Name |  English  | Maths | Total |
    |      | Th | Prac |       |       |
----------------------------------------
    |      |    |      |       |       |
    |      |    |      |       |       |
    |      |    |      |       |       |
----------------------------------------

Kinda urgent, if somebody may help.
Posted

1 solution

e.g
suppose in grid this columns,
Mon1
Mon2
tues1
tues2
wed1
wed2

VB
Private Sub dataGridView1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles DataGridView1.Paint
      Try
          Dim colList ="Mon,tues,wed"
          Dim clst As String() = colList.ToString.Split(",")
          Dim j As Integer = 0

          While j < DataGridView1.ColumnCount - 1 '10
              Dim r1 As Rectangle = Me.DataGridView1.GetCellDisplayRectangle(j, -1, True) 'see -1 that is row=-1 means header cell
              Dim w2 As Integer = Me.DataGridView1.GetCellDisplayRectangle(j + 1, -1, True).Width
              r1.X += 0
              r1.Y += 0
              '            r1.Width = r1.Width + w2 -2
              r1.Width = ((DataGridView1.ColumnCount / clst.Count) * w2)
              r1.Height = r1.Height / 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(clst(j / ((DataGridView1.ColumnCount / (clst.Count + 1)) + 1)), Me.DataGridView1.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor), r1, format)
              j += (DataGridView1.ColumnCount / clst.Count)
          End While
      Catch ex As Exception
      End Try
    End Sub


Happy Coding!
:)
 
Share this answer
 
v3
Comments
Anaya Upadhyay 8-Oct-12 7:41am    
I was asking for datagridviewcolumn header merging not the cells.....
Help...
Aarti Meswania 8-Oct-12 7:52am    
use row=-1 it will give you header cell

Aarti Meswania 9-Oct-12 7:03am    
create grid add this columns
Mon1
Mon2
tues1
tues2
wed1
wed2

and then apply function that i give in paint event it is giving output or not?
like this
mon tues wed
1 | 2 1 | 2 1 | 2
Anaya Upadhyay 9-Oct-12 7:12am    
Sorry, my mistake, i slightly overused a variable. I got the output correct. Thank you for your help... :)
Aarti Meswania 9-Oct-12 7:13am    
it's ok it happens :)
glad to help you! dear :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900