?? gridviewhelper.vb
字號(hào):
?'------------------------------------------------------------------------------------------
' Copyright ? 2006 Agrinei Sousa [www.agrinei.com]
'
' Esse código fonte é fornecido sem garantia de qualquer tipo.
' Sinta-se livre para utilizá-lo, modificá-lo e distribuí-lo,
' inclusive em aplica??es comerciais.
' é altamente desejável que essa mensagem n?o seja removida.
'------------------------------------------------------------------------------------------
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Delegate Sub FooterEvent(ByVal row As GridViewRow)
''' <summary>
''' A class to allow you to add summaries and groups to a GridView, easily!
''' </summary>
Public Class GridViewHelper
#Region "Fields"
Private mGrid As GridView
Private mGeneralSummaries As GridViewSummaryList
Private mGroups As GridViewGroupList
Private useFooter As Boolean
Private groupSortDir As SortDirection
#End Region
Public ReadOnly Property Groups() As GridViewGroupList
Get
Return mGroups
End Get
End Property
Public ReadOnly Property GeneralSummaries() As GridViewSummaryList
Get
Return mGeneralSummaries
End Get
End Property
#Region "Messages"
Private Const USE_ADEQUATE_METHOD_TO_REGISTER_THE_SUMMARY As String = "Use adequate method to register a summary with custom operation."
Private Const GROUP_NOT_FOUND As String = "Group {0} not found. Please register the group before the summary."
Private Const INVALID_SUMMARY As String = "Invalid summary."
Private Const SUPPRESS_GROUP_ALREADY_DEFINED As String = "A suppress group is already defined. You can't define suppress AND summary groups simultaneously"
Private Const ONE_GROUP_ALREADY_REGISTERED As String = "At least a group is already defined. A suppress group can't coexist with other groups"
#End Region
#Region "Events"
''' <summary>
''' Event triggered when a new group starts
''' </summary>
Public Event GroupStart As GroupEvent
''' <summary>
''' Event triggered when a group ends
''' </summary>
Public Event GroupEnd As GroupEvent
''' <summary>
''' Event triggered after a row for group header be inserted
''' </summary>
Public Event GroupHeader As GroupEvent
''' <summary>
''' Event triggered after a row for group summary be inserted
''' </summary>
Public Event GroupSummary As GroupEvent
''' <summary>
''' Event triggered after the general summaries be generated
''' </summary>
Public Event GeneralSummary As FooterEvent
''' <summary>
''' Event triggered when the footer is databound
''' </summary>
Public Event FooterDataBound As FooterEvent
#End Region
#Region "Constructors"
Public Sub New(ByVal grd As GridView)
Me.New(grd, False, SortDirection.Ascending)
End Sub
Public Sub New(ByVal grd As GridView, ByVal useFooterForGeneralSummaries As Boolean)
Me.New(grd, useFooterForGeneralSummaries, SortDirection.Ascending)
End Sub
Public Sub New(ByVal grd As GridView, ByVal useFooterForGeneralSummaries As Boolean, ByVal groupSortDirection As SortDirection)
Me.mGrid = grd
Me.useFooter = useFooterForGeneralSummaries
Me.groupSortDir = groupSortDirection
Me.mGeneralSummaries = New GridViewSummaryList()
Me.mGroups = New GridViewGroupList()
AddHandler Me.mGrid.RowDataBound, AddressOf RowDataBoundHandler
End Sub
#End Region
#Region "RegisterSummary overloads"
Public Function RegisterSummary(ByVal column As String, ByVal operation As SummaryOperation) As GridViewSummary
Return Me.RegisterSummary(column, [String].Empty, operation)
End Function
Public Function RegisterSummary(ByVal column As String, ByVal formatString As String, ByVal operation As SummaryOperation) As GridViewSummary
If operation = SummaryOperation.[Custom] Then
Throw New Exception(USE_ADEQUATE_METHOD_TO_REGISTER_THE_SUMMARY)
End If
' TO DO: Perform column validation...
Dim s As New GridViewSummary(column, formatString, operation, Nothing)
mGeneralSummaries.Add(s)
' if general summaries are displayed in the footer, it must be set to visible
If useFooter Then
mGrid.ShowFooter = True
End If
Return s
End Function
Public Function RegisterSummary(ByVal column As String, ByVal operation As SummaryOperation, ByVal groupName As String) As GridViewSummary
Return Me.RegisterSummary(column, [String].Empty, operation, groupName)
End Function
Public Function RegisterSummary(ByVal column As String, ByVal formatString As String, ByVal operation As SummaryOperation, ByVal groupName As String) As GridViewSummary
If operation = SummaryOperation.[Custom] Then
Throw New Exception(USE_ADEQUATE_METHOD_TO_REGISTER_THE_SUMMARY)
End If
Dim group As GridViewGroup = mGroups(groupName)
If group Is Nothing Then
Throw New Exception([String].Format(GROUP_NOT_FOUND, groupName))
End If
' TO DO: Perform column validation...
Dim s As New GridViewSummary(column, formatString, operation, group)
group.AddSummary(s)
Return s
End Function
Public Function RegisterSummary(ByVal column As String, ByVal operation As CustomSummaryOperation, ByVal getResult As SummaryResultMethod) As GridViewSummary
Return RegisterSummary(column, [String].Empty, operation, getResult)
End Function
Public Function RegisterSummary(ByVal column As String, ByVal formatString As String, ByVal operation As CustomSummaryOperation, ByVal getResult As SummaryResultMethod) As GridViewSummary
' TO DO: Perform column validation...
Dim s As New GridViewSummary(column, formatString, operation, getResult, Nothing)
mGeneralSummaries.Add(s)
' if general summaries are displayed in the footer, it must be set to visible
If useFooter Then
mGrid.ShowFooter = True
End If
Return s
End Function
Public Function RegisterSummary(ByVal column As String, ByVal operation As CustomSummaryOperation, ByVal getResult As SummaryResultMethod, ByVal groupName As String) As GridViewSummary
Return RegisterSummary(column, [String].Empty, operation, getResult, groupName)
End Function
Public Function RegisterSummary(ByVal column As String, ByVal formatString As String, ByVal operation As CustomSummaryOperation, ByVal getResult As SummaryResultMethod, ByVal groupName As String) As GridViewSummary
Dim group As GridViewGroup = mGroups(groupName)
If group Is Nothing Then
Throw New Exception([String].Format(GROUP_NOT_FOUND, groupName))
End If
' TO DO: Perform column validation...
Dim s As New GridViewSummary(column, formatString, operation, getResult, group)
group.AddSummary(s)
Return s
End Function
Public Function RegisterSummary(ByVal s As GridViewSummary) As GridViewSummary
If Not s.Validate() Then
Throw New Exception(INVALID_SUMMARY)
End If
If s.Group Is Nothing Then
' if general summaries are displayed in the footer, it must be set to visible
If useFooter Then
mGrid.ShowFooter = True
End If
mGeneralSummaries.Add(s)
ElseIf Not s.Group.ContainsSummary(s) Then
s.Group.AddSummary(s)
End If
Return s
End Function
#End Region
#Region "RegisterGroup overloads"
Public Function RegisterGroup(ByVal column As String, ByVal auto As Boolean, ByVal hideGroupColumns As Boolean) As GridViewGroup
Dim cols As String() = New String(0) {column}
Return RegisterGroup(cols, auto, hideGroupColumns)
End Function
Public Function RegisterGroup(ByVal columns As String(), ByVal auto As Boolean, ByVal hideGroupColumns As Boolean) As GridViewGroup
If HasSuppressGroup() Then
Throw New Exception(SUPPRESS_GROUP_ALREADY_DEFINED)
End If
' TO DO: Perform column validation...
Dim g As New GridViewGroup(columns, auto, hideGroupColumns)
mGroups.Add(g)
If hideGroupColumns Then
For i As Integer = 0 To mGrid.Columns.Count - 1
For j As Integer = 0 To columns.Length - 1
If GetDataFieldName(mGrid.Columns(i)).ToLower() = columns(j).ToLower() Then
mGrid.Columns(i).Visible = False
End If
Next
Next
End If
Return g
End Function
#End Region
#Region "SetSuppressGroup overloads"
Public Function SetSuppressGroup(ByVal column As String) As GridViewGroup
Dim cols As String() = New String(0) {column}
Return SetSuppressGroup(cols)
End Function
Public Function SetSuppressGroup(ByVal columns As String()) As GridViewGroup
If mGroups.Count > 0 Then
Throw New Exception(ONE_GROUP_ALREADY_REGISTERED)
End If
' TO DO: Perform column validation...
Dim g As New GridViewGroup(columns, True, False, False, False)
mGroups.Add(g)
' Disable paging because pager works in datarows that
' will be suppressed
mGrid.AllowPaging = False
Return g
End Function
#End Region
#Region "Private Helper functions"
Private Function GetSequentialGroupColumns() As String
Dim ret As String = [String].Empty
For Each g As GridViewGroup In mGroups
ret += g.Name.Replace("+"C, ","C) + ","
Next
Return ret.Substring(0, ret.Length - 1)
End Function
''' <summary>
''' Compares the actual group values with the values of the current dataitem
''' </summary>
''' <param name="g"></param>
''' <param name="dataitem"></param>
''' <returns></returns>
Private Function EvaluateEquals(ByVal g As GridViewGroup, ByVal dataitem As Object) As Boolean
' The values wasn't initialized
If g.ActualValues Is Nothing Then
Return False
End If
For i As Integer = 0 To g.Columns.Length - 1
If g.ActualValues(i) Is Nothing AndAlso DataBinder.Eval(dataitem, g.Columns(i)) IsNot Nothing Then
Return False
End If
If g.ActualValues(i) IsNot Nothing AndAlso DataBinder.Eval(dataitem, g.Columns(i)) Is Nothing Then
Return False
End If
If Not g.ActualValues(i).Equals(DataBinder.Eval(dataitem, g.Columns(i))) Then
Return False
End If
Next
Return True
End Function
Private Function HasSuppressGroup() As Boolean
For Each g As GridViewGroup In mGroups
If g.IsSuppressGroup Then
Return True
End If
Next
Return False
End Function
Private Function HasAutoSummary(ByVal list As List(Of GridViewSummary)) As Boolean
For Each s As GridViewSummary In list
If s.Automatic Then
Return True
End If
Next
Return False
End Function
Private Function GetGroupRowValues(ByVal g As GridViewGroup, ByVal dataitem As Object) As Object()
Dim values As Object() = New Object(g.Columns.Length - 1) {}
For i As Integer = 0 To g.Columns.Length - 1
values(i) = DataBinder.Eval(dataitem, g.Columns(i))
Next
Return values
End Function
''' <summary>
''' Inserts a grid row. Only cells required for the summary results
''' will be created (except if GenerateAllCellsOnSummaryRow is true).
''' The group will be checked for columns with summary
''' </summary>
''' <param name="beforeRow"></param>
''' <param name="g"></param>
''' <returns></returns>
Private Function InsertGridRow(ByVal beforeRow As GridViewRow, ByVal g As GridViewGroup) As GridViewRow
Dim colspan As Integer
Dim cell As TableCell
Dim tcArray As TableCell()
Dim visibleColumns As Integer = Me.GetVisibleColumnCount()
Dim tbl As Table = DirectCast(mGrid.Controls(0), Table)
Dim newRowIndex As Integer = tbl.Rows.GetRowIndex(beforeRow)
Dim newRow As New GridViewRow(newRowIndex, newRowIndex, DataControlRowType.DataRow, DataControlRowState.Normal)
If g IsNot Nothing AndAlso (g.IsSuppressGroup OrElse g.GenerateAllCellsOnSummaryRow) Then
' Create all the table cells
tcArray = New TableCell(visibleColumns - 1) {}
For i As Integer = 0 To visibleColumns - 1
cell = New TableCell()
cell.ApplyStyle(mGrid.Columns(GetRealIndexFromVisibleColumnIndex(i)).ItemStyle)
cell.Text = " "
tcArray(i) = cell
Next
Else
' Create only the required table cells
colspan = 0
Dim tcc As New List(Of TableCell)()
For i As Integer = 0 To mGrid.Columns.Count - 1
If ColumnHasSummary(i, g) Then
If colspan > 0 Then
cell = New TableCell()
cell.Text = " "
cell.ColumnSpan = colspan
tcc.Add(cell)
colspan = 0
End If
' insert table cell and copy the style
cell = New TableCell()
cell.ApplyStyle(mGrid.Columns(i).ItemStyle)
tcc.Add(cell)
ElseIf mGrid.Columns(i).Visible Then
' A visible column that will have no cell because has
' no summary. So we increase the colspan...
colspan += 1
End If
Next
If colspan > 0 Then
cell = New TableCell()
cell.Text = " "
cell.ColumnSpan = colspan
tcc.Add(cell)
colspan = 0
End If
tcArray = New TableCell(tcc.Count - 1) {}
tcc.CopyTo(tcArray)
End If
newRow.Cells.AddRange(tcArray)
tbl.Controls.AddAt(newRowIndex, newRow)
Return newRow
End Function
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -