Can i achieve grouping and theming in DataGridView RRS feed

  • Question

  • Hi, 

    I want to bind the data retrieved from database to DataGridView in my Windows Forms Application...

    My requirement is to group this data..Please see attached image for more details...

    Please let me know how can i achieve this using DataGridView or do i need to use any other WinForms control...


    Tuesday, March 19, 2013 6:33 AM


  • Hello,

    For coloring background of rows you would need to write logic in CellFormatting event of the DataGridView. For grouping a start would be using the DataGridView controls below.

    A better idea especially in regards the all your requirements is to use a third party grid such as the one below


    ''' <summary>
    ''' Original author
    ''' http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq8
    ''' </summary>
    ''' <remarks>
    ''' Original code was in C Sharp, I converted and tweaked some code
    ''' which did not compile under VB.NET
    ''' </remarks>
    Public Class GroupByGrid
        Inherits DataGridView
        Protected Overrides Sub OnCellFormatting(ByVal args As DataGridViewCellFormattingEventArgs)
            ' First row always displays
            If args.RowIndex = 0 Then
            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
                Return False
            End If
        End Function
        Protected Overrides Sub OnCellPainting(ByVal args As DataGridViewCellPaintingEventArgs)
            args.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None
            ' Ignore column and row headers and first row
            If args.RowIndex < 1 OrElse args.ColumnIndex < 0 Then
            End If
            If IsRepeatedCellValue(args.RowIndex, args.ColumnIndex) Then
                args.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None
                args.AdvancedBorderStyle.Top = AdvancedCellBorderStyle.Top
            End If
        End Sub
    End Class

    Enjoy life

    • Proposed as answer by Chester Hong Monday, March 25, 2013 8:59 AM
    • Marked as answer by Chester Hong Tuesday, April 2, 2013 10:42 AM
    Tuesday, March 19, 2013 11:44 AM