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:


Edited by xuexue

Votes + Comments
Clear question!
3 Years
Discussion Span
Last Post by ddanbe

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 :)


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!

Edited by ddanbe: correction

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.