📄 gridviewhelper.vb
字号:
#End Region
#Region "Core"
Private Sub RowDataBoundHandler(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
For Each g As GridViewGroup In mGroups
' The last group values are caught here
If e.Row.RowType = DataControlRowType.Footer Then
g.CalculateSummaries()
GenerateGroupSummary(g, e.Row)
RaiseEvent GroupEnd(g.Name, g.ActualValues, e.Row)
ElseIf e.Row.RowType = DataControlRowType.DataRow Then
ProcessGroup(g, e)
If g.IsSuppressGroup Then
e.Row.Visible = False
End If
ElseIf e.Row.RowType = DataControlRowType.Pager Then
' Workaround to strange behavior (ColumnSpan not rendered)
' when AllowPaging=true
' Found at: http://aspadvice.com/blogs/joteke/archive/2006/02/11/15130.aspx
Dim originalCell As TableCell = e.Row.Cells(0)
Dim newCell As New TableCell()
newCell.Visible = False
e.Row.Cells.AddAt(0, newCell)
originalCell.ColumnSpan = Me.GetVisibleColumnCount()
End If
Next
' This will deal only with general summaries
For Each s As GridViewSummary In mGeneralSummaries
If e.Row.RowType = DataControlRowType.Header Then
' Essentially this isn't required, but it prevents wrong calc
' in case of RowDataBound event be called twice (for each row)
s.Reset()
ElseIf e.Row.RowType = DataControlRowType.DataRow Then
s.AddValue(DataBinder.Eval(e.Row.DataItem, s.Column))
ElseIf e.Row.RowType = DataControlRowType.Footer Then
s.Calculate()
End If
Next
If e.Row.RowType = DataControlRowType.Footer Then
' Automatic generation of summary
GenerateGeneralSummaries(e)
' Triggers event footerdatabound
RaiseEvent FooterDataBound(e.Row)
End If
End Sub
Private Sub ProcessGroup(ByVal g As GridViewGroup, ByVal e As GridViewRowEventArgs)
Dim groupHeaderText As String = [String].Empty
' Check if it's still in the same group values
If Not EvaluateEquals(g, e.Row.DataItem) Then
' Check if a group ends or if it is the first group values starting...
If g.ActualValues IsNot Nothing Then
g.CalculateSummaries()
GenerateGroupSummary(g, e.Row)
' Triggers event GroupEnd
RaiseEvent GroupEnd(g.Name, g.ActualValues, e.Row)
End If
' Another group values starts now
g.Reset()
g.SetActualValues(GetGroupRowValues(g, e.Row.DataItem))
' If group is automatic inserts a group header
If g.Automatic Then
For v As Integer = 0 To g.ActualValues.Length - 1
If g.ActualValues(v) Is Nothing Then
Continue For
End If
groupHeaderText += g.ActualValues(v).ToString()
If g.ActualValues.Length - v > 1 Then
groupHeaderText += " - "
End If
Next
Dim newRow As GridViewRow = InsertGridRow(e.Row)
newRow.Cells(0).Text = groupHeaderText
' Triggers event GroupHeader
RaiseEvent GroupHeader(g.Name, g.ActualValues, newRow)
End If
' Triggers event GroupStart
RaiseEvent GroupStart(g.Name, g.ActualValues, e.Row)
End If
g.AddValueToSummaries(e.Row.DataItem)
End Sub
Private Function GetFormatedString(ByVal preferredFormat As String, ByVal secondFormat As String, ByVal value As Object) As String
Dim formatx As String = preferredFormat
If IsNothing(value) Or IsDBNull(value) Then Return ""
If formatx.Length = 0 Then
formatx = secondFormat
End If
If formatx.Length > 0 Then
Dim t As String = Format(Integer.Parse(value), "# ### ##0")
If t = formatx Then t = ""
Return t
Else
Return value.ToString()
End If
End Function
Private Sub GenerateGroupSummary(ByVal g As GridViewGroup, ByVal row As GridViewRow)
Dim colIndex As Integer
Dim colValue As Object
If Not HasAutoSummary(g.Summaries) AndAlso Not HasSuppressGroup() Then
Return
End If
' Inserts a new row
Dim newRow As GridViewRow = InsertGridRow(row, g)
For Each s As GridViewSummary In g.Summaries
If s.Automatic Then
colIndex = GetVisibleColumnIndex(s.Column)
colIndex = ResolveCellIndex(newRow, colIndex)
newRow.Cells(colIndex).Text = Me.GetFormatedString(s.FormatString, Me.GetColumnFormat(GetColumnIndex(s.Column)), s.Value)
End If
Next
' If it is a suppress group must set the grouped values in the cells
' of the inserted row
If g.IsSuppressGroup Then
For i As Integer = 0 To g.Columns.Length - 1
colValue = g.ActualValues(i)
If colValue IsNot Nothing Then
colIndex = GetVisibleColumnIndex(g.Columns(i))
colIndex = ResolveCellIndex(newRow, colIndex)
newRow.Cells(colIndex).Text = colValue.ToString()
End If
Next
End If
RaiseEvent GroupSummary(g.Name, g.ActualValues, newRow)
' Triggers event GroupSummary
End Sub
''' <summary>
''' Generates the general summaries in the grid.
''' </summary>
''' <param name="e">GridViewRowEventArgs</param>
Private Sub GenerateGeneralSummaries(ByVal e As GridViewRowEventArgs)
Dim colIndex As Integer
Dim row As GridViewRow
If Not HasAutoSummary(Me.mGeneralSummaries) Then
RaiseEvent GeneralSummary(e.Row)
' Triggers event GeneralSummary
Return
End If
If useFooter Then
row = e.Row
Else
row = InsertGridRow(e.Row, Nothing)
End If
For Each s As GridViewSummary In mGeneralSummaries
If Not s.Automatic Then
Continue For
End If
If useFooter Then
colIndex = GetColumnIndex(s.Column)
Else
colIndex = GetVisibleColumnIndex(s.Column)
End If
colIndex = ResolveCellIndex(row, colIndex)
row.Cells(colIndex).Text = Me.GetFormatedString(s.FormatString, Me.GetColumnFormat(GetColumnIndex(s.Column)), s.Value)
Next
RaiseEvent GeneralSummary(row)
' Triggers event GeneralSummary
End Sub
''' <summary>
''' Identifies the equivalent index on a row that contains cells with colspan
''' </summary>
''' <param name="row"></param>
''' <param name="colIndex"></param>
''' <returns></returns>
Private Function ResolveCellIndex(ByVal row As GridViewRow, ByVal colIndex As Integer) As Integer
Dim colspansum As Integer = 0
Dim realIndex As Integer
For i As Integer = 0 To row.Cells.Count - 1
realIndex = i + colspansum
If realIndex = colIndex Then
Return i
End If
If row.Cells(i).ColumnSpan > 1 Then
colspansum = colspansum + row.Cells(i).ColumnSpan - 1
End If
Next
Return -1
End Function
Private Function ColumnHasSummary(ByVal colindex As Integer, ByVal g As GridViewGroup) As Boolean
Dim list As List(Of GridViewSummary)
Dim column As String = Me.GetDataFieldName(mGrid.Columns(colindex))
If g Is Nothing Then
list = Me.mGeneralSummaries
Else
list = g.Summaries
End If
For Each s As GridViewSummary In list
If column.ToLower() = s.Column.ToLower() Then
Return True
End If
Next
Return False
End Function
Private Function ColumnHasSummary(ByVal column As String, ByVal g As GridViewGroup) As Boolean
Dim list As List(Of GridViewSummary)
If g Is Nothing Then
list = Me.mGeneralSummaries
Else
list = g.Summaries
End If
For Each s As GridViewSummary In list
If column.ToLower() = s.Column.ToLower() Then
Return True
End If
Next
Return False
End Function
#End Region
#Region "Public Helper functions"
Public Function GetRealIndexFromVisibleColumnIndex(ByVal visibleIndex As Integer) As Integer
Dim visibles As Integer = 0
For i As Integer = 0 To mGrid.Columns.Count - 1
If mGrid.Columns(i).Visible Then
If visibleIndex = visibles Then
Return i
End If
visibles += 1
End If
Next
' Not found....
Return -1
End Function
Public Function GetVisibleColumnIndex(ByVal columnName As String) As Integer
Dim visibles As Integer = 0
For i As Integer = 0 To mGrid.Columns.Count - 1
If GetDataFieldName(mGrid.Columns(i)).ToLower() = columnName.ToLower() Then
Return visibles
End If
If mGrid.Columns(i).Visible Then
visibles += 1
End If
Next
' Not found....
Return -1
End Function
Public Function GetColumnIndex(ByVal columnName As String) As Integer
For i As Integer = 0 To mGrid.Columns.Count - 1
If GetDataFieldName(mGrid.Columns(i)).ToLower() = columnName.ToLower() Then
Return i
End If
Next
' Not found....
Return -1
End Function
Public Function GetDataFieldName(ByVal field As DataControlField) As String
' TO DO: Enable search in HyperLinkField, ButtonField...
If TypeOf field Is BoundField Then
Return TryCast(field, BoundField).DataField
Else
' It hopes that SortExpression is set (and it's equal to column name)
Return field.SortExpression
End If
End Function
Public Function GetColumnFormat(ByVal colIndex As Integer) As String
' TO DO: Enable search in HyperLinkField, ButtonField...
If TypeOf mGrid.Columns(colIndex) Is BoundField Then
Return TryCast(mGrid.Columns(colIndex), BoundField).DataFormatString
Else
Return [String].Empty
End If
End Function
Public Function GetVisibleColumnCount() As Integer
Dim ret As Integer = 0
For i As Integer = 0 To mGrid.Columns.Count - 1
If mGrid.Columns(i).Visible Then
ret += 1
End If
Next
Return ret
End Function
''' <summary>
''' This method must be called to hide columns that doesn't
''' have any summary operation when we are using a suppress group
''' </summary>
Public Sub HideColumnsWithoutGroupSummary()
Dim colname As String
Dim colChecked As Boolean
For Each dcf As DataControlField In mGrid.Columns
colChecked = False
colname = GetDataFieldName(dcf).ToLower()
For Each g As GridViewGroup In mGroups
For j As Integer = 0 To g.Columns.Length - 1
' Check if it's part of the group columns
If colname = g.Columns(j).ToLower() Then
colChecked = True
Exit For
End If
Next
If colChecked Then
Exit For
End If
' Check if it's part of a group summary
colChecked = ColumnHasSummary(colname, g)
If colChecked Then
Exit For
End If
Next
If colChecked Then
Continue For
End If
dcf.Visible = False
Next
End Sub
''' <summary>
''' Legacy name...
''' </summary>
Public Sub SetInvisibleColumnsWithoutGroupSummary()
Me.HideColumnsWithoutGroupSummary()
End Sub
''' <summary>
''' Inserts a grid row with one cell only
''' </summary>
''' <param name="beforeRow"></param>
''' <returns></returns>
Public Function InsertGridRow(ByVal beforeRow As GridViewRow) As GridViewRow
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)
newRow.Cells.Add(New TableCell())
If visibleColumns > 1 Then
newRow.Cells(0).ColumnSpan = visibleColumns
End If
tbl.Controls.AddAt(newRowIndex, newRow)
Return newRow
End Function
Public Sub ApplyGroupSort()
mGrid.Sort(Me.GetSequentialGroupColumns(), groupSortDir)
End Sub
#End Region
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -