TelerikDeveloper.com

TelerikDeveloper.com

sgray128 3/3/2014 8:43:15 AM

Winforms - Gridview - Summary Rows

This neat code sample will show how to code summary rows in a Winforms GridView            


The work is in two parts. In the SetupGrid method we add a GridViewSummaryRowItem to the grid definition.

In the ViewCellFormatting event we add formatting (align right and bold)

 

Sub SetupGrid()
        Dim oTelerikGrid As New FPTelerikCommon.TelerikGrid
 
        'add new columns by type and name to the grid...verified in SQL server
        Me.RadGridView1.Columns.Add(oTelerikGrid.createGridViewTextBoxColumn("", "RowID", 11, 140, False, False))
        Me.RadGridView1.Columns.Add(oTelerikGrid.createGridViewTextBoxColumn("", "CUSTNMBR", 21, 140, False, False))
        Me.RadGridView1.Columns.Add(oTelerikGrid.createGridViewTextBoxColumn("Document/PO", "DOCNUMBER", 21, 140))
        Me.RadGridView1.Columns.Add(oTelerikGrid.createGridViewDecimalColumn("Payment Received", "Amount", "{0:F2}", 100))
        Me.RadGridView1.Columns.Add(oTelerikGrid.createGridViewDecimalColumn("Applied Amt", "apfrmaplyamt", "{0:F2}", 90, True, True))
        Me.RadGridView1.Columns.Add(oTelerikGrid.createGridViewDecimalColumn("Unapplied", "unappliedamt", "{0:F2}", 90, True, True))
        DirectCast(Me.RadGridView1.Columns("DOCNUMBER"), GridViewTextBoxColumn).ColumnCharacterCasing = CharacterCasing.Upper
 
        'set the summary row
        Dim summary1 As New GridViewSummaryItem("Amount", "{0:F2}", GridAggregateFunction.Sum)
        Dim summary2 As New GridViewSummaryItem("apfrmaplyamt", "{0:F2}", GridAggregateFunction.Sum)
        Dim summary3 As New GridViewSummaryItem("unappliedamt", "{0:F2}", GridAggregateFunction.Sum)
        Dim summaryRowItem As New GridViewSummaryRowItem(New GridViewSummaryItem() {summary1, summary2, summary3})
        Me.RadGridView1.SummaryRowsTop.Add(summaryRowItem)
 
        'set the grid properties
        Me.RadGridView1.EnableGrouping = False
        Me.RadGridView1.AllowAddNewRow = True
        Me.RadGridView1.AllowDeleteRow = True
        Me.RadGridView1.AllowEditRow = True
        Me.RadGridView1.EnableFiltering = False
        Me.RadGridView1.MasterTemplate.EnableSorting = True
 
        'set the sorting
        Dim oSortDescriptor As New Telerik.WinControls.Data.SortDescriptor
        oSortDescriptor.PropertyName = "RowID"
        oSortDescriptor.Direction = System.ComponentModel.ListSortDirection.Descending
        Me.RadGridView1.SortDescriptors.Add(oSortDescriptor)
 
    End Sub
 
    Private Sub RadGridView1_ViewCellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles RadGridView1.ViewCellFormatting
 
        If TypeOf e.CellElement Is Telerik.WinControls.UI.GridSummaryCellElement Then
            e.CellElement.TextAlignment = ContentAlignment.MiddleRight
            e.CellElement.Font = New System.Drawing.Font(e.CellElement.Font, System.Drawing.FontStyle.Bold)
        End If
 
    End Sub