Merge cells in DataGridView VB.NET

5

The question I have is, how can I combine cells in a DataGridView?

In my VB.NET project I have a DataGridView that shows information from a table in my MySQL database in the following way:

And what I want is to combine the celas of the first column so that something is like this:

How do I do it?

The tables of the images are made in Excel just to give the example, it should be clarified that I only show 2 services but in reality they are 30 services.

    
asked by Jose Dario Correa 12.05.2016 в 16:55
source

3 answers

1

This is the perfect solution to my problem, I share it so that everyone has it.

In the Load event of Form the following statement is placed:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DataGridView1.AutoGenerateColumns = False
End Sub

In the CellPainting event of the DataGridView we placed the following:

Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None
    If (e.RowIndex < 1 Or e.ColumnIndex < 0) Then
        Return
    End If

    If (IsTheSameCellValue(e.ColumnIndex, e.RowIndex)) Then
        e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None
    Else
        e.AdvancedBorderStyle.Top = DataGridView1.AdvancedCellBorderStyle.Top
    End If
End Sub

In the CellFormatting event of the DataGridView we placed the following:

Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    If (e.RowIndex = 0) Then
        Return
    End If
    If (IsTheSameCellValue(e.ColumnIndex, e.RowIndex)) Then
        e.Value = ""
        e.FormattingApplied = True
    End If
End Sub

And additional we need to create a function:

Private Function IsTheSameCellValue(ByVal column As Integer, ByVal row As Integer) As Boolean
    Dim cell1 As DataGridViewCell = DataGridView1(column, row)
    Dim cell2 As DataGridViewCell = DataGridView1(column, row - 1)
    If (IsDBNull(cell1.Value) Or IsDBNull(cell2.Value)) Then
        Return False
    End If
    If (cell1.Value = cell2.Value) Then
        Return True
    Else
        Return False
    End If
End Function
    
answered by 13.05.2016 / 18:03
source
2

The DataGridView does not provide this functionality.

In CodeProject if you can find an implementation of a column type for DataGridView that allows you to do it:

DataGridVewTextBoxCell with Span Behavior

Basically, it creates a DataGridViewTextBoxColumnEx column type whose cells are of type DataGridViewTextBoxCellEx. These cells allow you to combine cells through their RowSpan and ColSpan properties.

An example of use:

    DataGridView1.Rows.Add("Bath/Shower", "Turn 1")
    DataGridView1.Rows.Add("Bath/Shower", "Turn 2")
    DataGridView1.Rows.Add("Bath/Shower", "Turn 3")
    DataGridView1.Rows.Add("Hair Care", "Turn 1")
    DataGridView1.Rows.Add("Hair Care", "Turn 2")
    DataGridView1.Rows.Add("Hair Care", "Turn 3")
    Dim cell As DataGridViewTextBoxCellEx = CType(DataGridView1(0, 0), DataGridViewTextBoxCellEx)
    cell.RowSpan = 3
    cell = CType(DataGridView1(0, 3), DataGridViewTextBoxCellEx)
    cell.RowSpan = 3

The result:

To use it:

  • Download the project code from the CodeProject website
  • The code is a solution with 3 projects. The one you are interested in is SpannedDataGridViewNet2. Add this project to your solution
  • In your Windows Forms project add a reference to this project and compile
  • When adding columns in your DataGridView you will have the option to choose a new column type: DataGridViewTextBoxColumnEx
  • The cells of these columns are of the DataGridViewTextBoxCellEx type that allow you to indicate values for their RowSpan and ColumnSpan properties as I put you in the example
  • answered by 12.05.2016 в 20:46
    1

    You could opt for an option like being

    How to Merge DataGridView Cell in Winforms

    in the event CellPainting of the DataGridView

    there validates if the value of the columan is equal to that of the previous cell, if it is it deletes the line of the cells using

     e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
    

    There is also another more complex way that GDI uses

    MERGING CELLS IN DATAGRIDVIEW

        
    answered by 12.05.2016 в 20:42