Добавить итог в VB.Net Gridview

Я не могу заставить сетку отображать общую сумму в нижнем колонтитуле

Я пробовал следующее:

<asp:GridView ID="GV" runat="server" 
    DataSourceID="SqlQuery" EmptyDataText="No data">
    <Columns>
        <asp:BoundField DataField="Weekday" FooterText=" " HeaderText="Weekday" />
        <asp:BoundField DataField="Volume" DataFormatString="{0:N0}" 
            FooterText="." HeaderText="Volume" />            
    </Columns>
 </asp:GridView>


Protected Sub GV_rowdatabound(sender As Object, e As GridViewRowEventArgs) Handles GV.RowDataBound
    Dim Volume as integer = 0
    For Each r As GridViewRow In GV.Rows
        If r.RowType = DataControlRowType.DataRow Then
            Volume = Volume + CDec(r.Cells(1).Text)
        End If
    Next
    GV.FooterRow.Cells(1).Text = Math.Round(Volume , 0)
End Sub

Это дает мне сообщение об ошибке:

В экземпляре объекта не задана ссылка на объект

Я последовал совету, приведенному на следующей странице, и изменил код: пытаясь получить полное представление сетки в asp < / а>

Sub GV_WeekSumary_rowcreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    Dim Volume as integer = 0
    For Each r As GridViewRow In GV.Rows
        If r.RowType = DataControlRowType.DataRow Then
            Volume = Volume + CDec(r.Cells(1).Text)
        End If
    Next

    If e.Row.RowType = DataControlRowType.Footer Then
        e.Row.Cells(1).Text = Math.Round(Volume , 0)
    End If
End Sub

Это не вызывает ошибки, но нижний колонтитул не показывает никакого значения.

Я пробовал также следующее:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim Volume as integer = 0
    For Each r As GridViewRow In GV.Rows
        If r.RowType = DataControlRowType.DataRow Then
            Volume = Volume + CDec(r.Cells(1).Text)
        End If
    Next
    GV.FooterRow.Cells(1).Text = Math.Round(Volume, 0)
    GV.DataBind()
End Sub

В нижнем колонтитуле все еще нет значения, но когда я его отлаживаю, я вижу, что нижнему колонтитулу присвоено нужное мне значение. Почему он не отображается на сайте?

Есть идеи, как я могу заставить это работать?


person Selrac    schedule 16.09.2013    source источник


Ответы (1)


Вы должны использовать событие DataBound.

Попробуй это:

Protected Sub GV_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GV.DataBound
    Dim Volume As Decimal = 0
    For Each r As GridViewRow In GV.Rows
        If r.RowType = DataControlRowType.DataRow Then
            Volume += Convert.ToDecimal(r.Cells(1).Text)
        End If
    Next
    GV.FooterRow.Cells(1).Text = Math.Round(Volume, 0).ToString()
End Sub
person Samiey Mehdi    schedule 16.09.2013
comment
Спасибо за ваш ответ. Это была опечатка, которую я исправил. Тем не менее, я не вижу данных на сайте - person Selrac; 16.09.2013
comment
Да, да !! Спасибо. Databound вместо rowbound, тогда - person Selrac; 16.09.2013