2012年2月29日水曜日

[ASP.NET]データグリッドフッターに合計行作成

From Evernote:

[ASP.NET]データグリッドフッターに合計行作成

データグリッドを作成した後、
最後の行にフッターを作成します。
イメージとしては、合計を表示するhtmlテーブルを定義して、
フッターに入れ込む感じです。


#Region "データグリッド合計フッター作成"
    ''' <summary>
    ''' 合計フッター作成
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub gvList_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles dgdKiban.RowDataBound

        Try
            Select Case e.Row.RowType
                Case DataControlRowType.Header
                    Dim myttalArray(10) As Integer
                    ViewState("mytotal") = myttalArray
                Case DataControlRowType.DataRow
                    CType(ViewState("mytotal"), Array)(1) += MC.ReplaceNaN(DataBinder.Eval(e.Row.DataItem, "以前生産数"), 0)
                    CType(ViewState("mytotal"), Array)(2) += MC.ReplaceNaN(DataBinder.Eval(e.Row.DataItem, "生産数"), 0)
                    CType(ViewState("mytotal"), Array)(3) += MC.ReplaceNaN(DataBinder.Eval(e.Row.DataItem, "累計"), 0)
                Case DataControlRowType.Footer
                    e.Row.BackColor = Drawing.Color.CadetBlue
                    e.Row.Height = 25
                    e.Row.Style.Item("text-align") = "right"
                    With e.Row.Cells(0)
                        .Text = "合計"
                        .Style.Item("font-weight") = "bold"
                        .Style.Item("text-align") = "left"
                    End With
                    Dim objLabel1 As New Label
                    With objLabel1
                        .ID = "GokeiIzenSeisanSu"
                        .Style.Item("font-weight") = "bold"
                        .Text = MC.FormatToComma(CType(ViewState("mytotal"), Array)(1))
                    End With
                    e.Row.Cells(2).Controls.Add(objLabel1)
                    Dim objLabel2 As New Label
                    With objLabel2
                        .ID = "GokeiSeisanSu"
                        .Style.Item("font-weight") = "bold"
                        .Text = MC.FormatToComma(CType(ViewState("mytotal"), Array)(2))
                    End With
                    e.Row.Cells(3).Controls.Add(objLabel2)
                    Dim objLabel3 As New Label
                    With objLabel3
                        .ID = "GokeiRuikei"
                        .Style.Item("font-weight") = "bold"
                        .Text = MC.FormatToComma(CType(ViewState("mytotal"), Array)(3))
                    End With
                    e.Row.Cells(4).Controls.Add(objLabel3)
            End Select

        Catch ex As Exception
            Throw ex
        End Try
    End Sub
#End Region

0 件のコメント:

コメントを投稿