none
How to group datagridview Rows in VB.NET RRS feed

  • Question

  • Hi guys.

    I have this problem on how to group rows in a datagridview. I want my datagridview to display data like image i uploaded, if you would mind to take a look at the image;

    I've already tried the code below, it works fine but i want to my datagridview retrieve data like the image above;

        Private Sub dgvCurriculum_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvCurriculum.CellFormatting
    
            If e.RowIndex > 0 And e.ColumnIndex = 0 Then
                If dgvCurriculum.Item(0, e.RowIndex - 1).Value = e.Value Then
                    e.Value = ""
                ElseIf e.RowIndex < dgvCurriculum.Rows.Count - 1 Then
                    dgvCurriculum.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.White
                End If
            End If
        End Sub
    I know it sounds simple but, i couldn't find another way on how to do it. I'm just new in vb.net.


    • Edited by Al Jamal Monday, July 24, 2017 7:14 AM
    Monday, July 24, 2017 7:10 AM

All replies

  • The best you can get here is as shown below. Otherwise you would need a custom third party DataGridView. The following link (I did many years ago) shows how to do as shown below. The code uses a custom (free) DataGridView which loads data from a xml file but could come from any viable data source such as a DataSet or DataTable for instance. The grouping is done on the first column feed to the DataGridView, in this case LastName.

    Public Class GroupByGrid
        Inherits DataGridView
    
        Protected Overrides Sub OnCellFormatting(ByVal args As DataGridViewCellFormattingEventArgs)
            MyBase.OnCellFormatting(args)
    
            ' First row always displays
            If args.RowIndex = 0 Then
                Return
            End If
    
            If IsRepeatedCellValue(args.RowIndex, args.ColumnIndex) Then
                args.Value = String.Empty
                args.FormattingApplied = True
            End If
        End Sub
        Private Function IsRepeatedCellValue(ByVal rowIndex As Integer, ByVal colIndex As Integer) As Boolean
            Dim currCell As DataGridViewCell = Rows(rowIndex).Cells(colIndex)
            Dim prevCell As DataGridViewCell = Rows(rowIndex - 1).Cells(colIndex)
    
            If (currCell.Value Is prevCell.Value) OrElse (currCell.Value IsNot Nothing AndAlso prevCell.Value IsNot Nothing AndAlso currCell.Value.ToString() = prevCell.Value.ToString()) Then
                Return True
            Else
                Return False
            End If
    
        End Function
        Protected Overrides Sub OnCellPainting(ByVal args As DataGridViewCellPaintingEventArgs)
    
            MyBase.OnCellPainting(args)
    
            args.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None
    
            ' Ignore column and row headers and first row
            If args.RowIndex < 1 OrElse args.ColumnIndex < 0 Then
                Return
            End If
    
            If IsRepeatedCellValue(args.RowIndex, args.ColumnIndex) Then
                args.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None
            Else
                args.AdvancedBorderStyle.Top = AdvancedCellBorderStyle.Top
            End If
    
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, July 24, 2017 10:29 AM
    Moderator
  • it's didn't work for me.
    i have the data, and i want to do grouping as like him, but its didnt work for me. its in the event or its public class ?
    Friday, December 7, 2018 3:36 AM
  • it's didn't work for me.
    i have the data, and i want to do grouping as like him, but its didnt work for me. its in the event or its public class ?

    "Its didn't work for me" tells us nothing, you will have to go into details. And as mentioned in my original reply this may not work for everyone, you may need a third party grid such as this one which I've used and works well.

    Example

    Dim descriptor1 As New GroupDescriptor()
    descriptor1.GroupNames.Add("Country", ListSortDirection.Ascending)
    descriptor1.GroupNames.Add("ContactTitle", ListSortDirection.Descending)
    Me.RadGridView1.GroupDescriptors.Add(descriptor1)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, December 7, 2018 12:14 PM
    Moderator