Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET Forms
I need to merge two column headers and give it a new header for Windows Forms

Like this:
 

</h2>
 
SNO | Name |  English  | Maths | Total |
<h2>    |      | Th | Prac |       |       |</h2>
 
<pre>
|      |    |      |       |       |
|      |    |      |       |       |
 

| | | | | |


Kinda urgent, if somebody may help.
Posted 8-Oct-12 0:52am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

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
 
<pre>
      While j &lt; DataGridView1.ColumnCount - 1 '10
          Dim r1 As Rectangle = Me.DataGridView1.GetCellDisplayRectangle(j, <u>-1</u>, 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</pre>

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

1 member at 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 at 9-Oct-12 7:12am
   
Sorry, my mistake, i slightly overused a variable. I got the output correct. Thank you for your help... :)
1 member at 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)

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 325
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200
0 OriginalGriff 465
1 Sergey Alexandrovich Kryukov 275
2 ProgramFOX 265
3 Maciej Los 245
4 Andreas Gieriet 200


Advertise | Privacy | Mobile
Web01 | 2.8.150331.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100