Hi! I've found this code in the net that merges rows successfully. However, the data is being written in the last row of the merge rows. What I want is that it should be written on the first instead. This is the code:

Using gridBrush As Brush = New SolidBrush(Me.dgSM.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)
    Using gridLinePen As Pen = New Pen(gridBrush)
        e.Graphics.FillRectangle(backColorBrush, e.CellBounds)

        If intRow < dgSM.Rows.Count - 1 AndAlso dgSM.Rows(intRow + 1).Cells(7).Value.ToString() = e.Value.ToString() Then
            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
            e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
        End If

        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)
        e.Handled = True
    End Using
End Using

This is the output:

90c267892d976ed95d7e7be33194207b90c267892d976ed95d7e7be33194207b

ddanbe commented: Clear question! +15

Have a look at the alignment property

Nope, this is a merging function. Actually, it does not merge the cells, it deletes the cells itself. Or if you could teach me another way to merge cells vertically in datagridview :)

Perhaps a look here might help?

Hi! Thanks for suggesting the link. Only problem is where should I put the code? Do I need to create a class and put the code there? After pasting the code in the class, how can I make it work?

GroupByGrid is a class that inherits DataGridView, so GroupByGrid IS a DataGridView with some overrides and an extra function.
In your code you should just change all references of DataGridView to GroupByGrid and you're back in busineness!

Hi xuexue, You can try this one

If e.RowIndex > 0 AndAlso DGTariffeAssegnate.Rows(e.RowIndex - 1).Cells(1).Value.ToString() = e.Value.ToString() Then
    Using gridBrush As Brush = New SolidBrush(Me.DGTariffeAssegnate.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor)
        Using gridLinePen As Pen = New Pen(gridBrush)
            e.Graphics.FillRectangle(backColorBrush, e.CellBounds.Left, e.CellBounds.Top - 1, e.CellBounds.Width - 1, e.CellBounds.Height)
            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Left, e.CellBounds.Bottom)
            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)
            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1)
            e.Handled = True
        End Using
    End Using
End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.