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:
 
----------------------------------------
SNO | Name |  English  | Maths | Total |
    |      | Th | Prac |       |       |
----------------------------------------
    |      |    |      |       |       |
    |      |    |      |       |       |
    |      |    |      |       |       |
----------------------------------------
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
 
          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!
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 Mathew Soji 330
1 BillWoodruff 260
2 Sergey Alexandrovich Kryukov 240
3 OriginalGriff 216
4 Afzaal Ahmad Zeeshan 208
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,028
3 Manas Bhardwaj 4,539
4 Maciej Los 3,845


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2014
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