Generate double header columns in a form datagridview (Windows form) in VB.Net [closed]

0

Good afternoon, I'm putting together a form where you import a SQl Server table into a datagridview

I want to build double header as shown in the image.

    
asked by Esteban555 10.11.2017 в 00:37
source

1 answer

-1

The headers are created using the "Paint" method of the DataGridView, for your specific case you should generalize with a cycle the creation of the grouping headers

Here's how to implement it:

    Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'DataSource
        Me.CMS_AttachmentTableAdapter.Fill(Me.KenticoEIBMDataSet.CMS_Attachment)
        AjustarPropiedadesGrid()
    End Sub

    Private Sub AjustarPropiedadesGrid()
        DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
        'Alto de los headers, se coloca el doble del alto para poder adicionar los nuevos headers
        DataGridView1.ColumnHeadersHeight = DataGridView1.ColumnHeadersHeight * 2
        DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
    End Sub

    Private Sub DataGridView1_Paint(sender As Object, e As PaintEventArgs) Handles DataGridView1.Paint
        Dim formatoTexto As New StringFormat()
        formatoTexto.Alignment = StringAlignment.Center
        formatoTexto.LineAlignment = StringAlignment.Center
        Dim alto As Integer

        Dim encabezadoID As Rectangle = DataGridView1.GetCellDisplayRectangle(0, -1, True)
        'Es el mismo para todos los encabezados
        alto = encabezadoID.Height / 2 - 2
        encabezadoID.X += 1
        encabezadoID.Y += 0
        encabezadoID.Width = encabezadoID.Width - 2
        encabezadoID.Height = alto
        e.Graphics.FillRectangle(New SolidBrush(Color.LightBlue), encabezadoID)
        e.Graphics.DrawString("", DataGridView1.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(Color.Black), encabezadoID, formatoTexto)

        Dim encabezadoDatosBasicos As Rectangle = DataGridView1.GetCellDisplayRectangle(1, -1, True)
        encabezadoDatosBasicos.Width = DataGridView1.GetCellDisplayRectangle(1, -1, True).Width + DataGridView1.GetCellDisplayRectangle(2, -1, True).Width + DataGridView1.GetCellDisplayRectangle(3, -1, True).Width + DataGridView1.GetCellDisplayRectangle(4, -1, True).Width - 2
        encabezadoDatosBasicos.Height = alto
        encabezadoDatosBasicos.X += 1
        encabezadoDatosBasicos.Y += 0
        e.Graphics.FillRectangle(New SolidBrush(Color.LightSteelBlue), encabezadoDatosBasicos)
        e.Graphics.DrawString("Datos Básicos", DataGridView1.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(Color.Black), encabezadoDatosBasicos, formatoTexto)


        Dim encabezadoDimensiones As Rectangle = DataGridView1.GetCellDisplayRectangle(5, -1, True)
        encabezadoDimensiones.Width = DataGridView1.GetCellDisplayRectangle(5, -1, True).Width + DataGridView1.GetCellDisplayRectangle(6, -1, True).Width - 2
        encabezadoDimensiones.Height = alto
        encabezadoDimensiones.X += 1
        encabezadoDimensiones.Y += 0
        e.Graphics.FillRectangle(New SolidBrush(Color.LightSlateGray), encabezadoDimensiones)
        e.Graphics.DrawString("Dimensiones", DataGridView1.ColumnHeadersDefaultCellStyle.Font, New SolidBrush(Color.Black), encabezadoDimensiones, formatoTexto)
    End Sub

    Private Sub DataGridView1_ColumnWidthChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles DataGridView1.ColumnWidthChanged
        Dim rectanguloAreaControl As Rectangle = DataGridView1.DisplayRectangle
        rectanguloAreaControl.Height = DataGridView1.ColumnHeadersHeight / 2
        Me.DataGridView1.Invalidate(rectanguloAreaControl)
    End Sub

    Private Sub DataGridView1_Scroll(sender As Object, e As ScrollEventArgs) Handles DataGridView1.Scroll
        Dim rectanguloAreaControl As Rectangle = DataGridView1.DisplayRectangle
        rectanguloAreaControl.Height = DataGridView1.ColumnHeadersHeight / 2
        Me.DataGridView1.Invalidate(rectanguloAreaControl)
    End Sub
End Class

The result of my example is as follows:

    
answered by 10.11.2017 / 04:31
source