12,395,319 members (66,842 online)
Rate this:
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 7-Oct-12 23:52pm

Rate this:

## Solution 1

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

```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

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!
v3

Help...
Aarti Meswania 8-Oct-12 7:52am

use row=-1 it will give you header cell

1 member 9-Oct-12 7:03am

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

Sorry, my mistake, i slightly overused a variable. I got the output correct. Thank you for your help... :)
1 member 9-Oct-12 7:13am

it's ok it happens :)

Top Experts
Last 24hrsThis month
 OriginalGriff 215 Jochen Arndt 130 Richard MacCutchan 90 Richard Deeming 80 khaled Ezzat 70
 OriginalGriff 5,998 Karthik Bangalore 2,382 ppolymorphe 2,350 F-ES Sitecore 1,877 Richard MacCutchan 1,707