Click here to Skip to main content
Sign Up to vote bad
good
See more: VB.NETForms
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:52


1 solution

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  
Comments
Anaya Upadhyay - 8 Oct '12 - 7:41
I was asking for datagridviewcolumn header merging not the cells..... Help...
Aarti Meswania - 8 Oct '12 - 7:52
use row=-1 it will give you header cell
1 member - 9 Oct '12 - 7:03
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:12
Sorry, my mistake, i slightly overused a variable. I got the output correct. Thank you for your help... :)
1 member - 9 Oct '12 - 7:13
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
Your Filters
Interested
Ignored
     
0 Mohammed Hameed 268
1 OriginalGriff 261
2 Sergey Alexandrovich Kryukov 188
3 Mayur_Panchal 153
4 Santhosh G_ 108
0 Sergey Alexandrovich Kryukov 8,216
1 OriginalGriff 6,271
2 CPallini 3,528
3 Rohan Leuva 2,703
4 Maciej Los 2,234


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