Solved

Gridview Footer Sum Doubles on UPDATE only.

Posted on 2015-01-29
4
319 Views
Last Modified: 2015-01-30
Hi Experts,

I'm at a lose on how to fix this problem.

I have a gridview that uses the RowDataBound to display the sum of the totals for each record in my grid.

Protected Sub GridServices_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridServices.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            _EstCostTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "EstimatedCost"))
            _ActCostTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ActualCost"))
            _ProposedTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Proposed"))
            _AdjustedTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Adjusted"))
            _CRDBTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CreditDebit"))
        End If

        If Convert.ToInt32(Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CreditDebit"))) < 0 Then
            e.Row.Cells(6).ForeColor = Drawing.Color.Red
        End If

        If e.Row.RowType = DataControlRowType.Footer Then
            ' Cell 0
            e.Row.Cells(1).Text = "Totals:"
            e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Right

            ' Cell 1 - Estimated Total
            e.Row.Cells(2).Text = _EstCostTotal.ToString("c")
            e.Row.Cells(2).HorizontalAlign = HorizontalAlign.Right
            ' e.Row.Cells(2).CssClass = "pGrandTotal"

            ' Cell 2 - Actual Total
            e.Row.Cells(3).Text = _ActCostTotal.ToString("c")
            e.Row.Cells(3).HorizontalAlign = HorizontalAlign.Right
            ' e.Row.Cells(3).CssClass = "pGrandTotal"

            ' Cell 3 - Proposed Total
            e.Row.Cells(4).Text = _ProposedTotal.ToString("c")
            e.Row.Cells(4).HorizontalAlign = HorizontalAlign.Right
            e.Row.Cells(4).ForeColor = Drawing.Color.DarkGreen
            ' e.Row.Cells(4).CssClass = "pGrandTotal"

            ' Cell 4 - Adjusted Total
            e.Row.Cells(5).Text = _AdjustedTotal.ToString("c")
            e.Row.Cells(5).HorizontalAlign = HorizontalAlign.Right
            e.Row.Cells(5).ForeColor = Drawing.Color.DarkGreen
            ' e.Row.Cells(5).CssClass = "pGrandTotal"

            ' Cell 4 - Credit Debit
            e.Row.Cells(6).Text = _CRDBTotal.ToString("c")
            e.Row.Cells(6).HorizontalAlign = HorizontalAlign.Right
            ' e.Row.Cells(6).CssClass = "pGrandTotal"

            If Convert.ToInt32(_CRDBTotal) < 0 Then
                e.Row.Cells(6).ForeColor = Drawing.Color.Red
            End If

        End If

    End Sub

Open in new window


When every I perform an either INSERT, DELETE or UPDATE the following subroutine is called:

Public Sub LoadGrid(ByVal worksheetID As Integer)

        Dim dt As DataTable = worksheetServicesLogic.GetServicesForWorksheet(worksheetID)

        If dt.Rows.Count > 0 Then

            ' Bind the main grid
            GridServices.DataSource = dt
            GridServices.DataBind()

        End If

    End Sub

Open in new window


When I INSERT a record, the summary is correct.
add.png
When I DELETE a record, the summary is correct.
delete.png
HOWEVER, when I UPDATE a record, the summary DOUBLES all the values???
update.png
Any idea as to what could be going on and more importantly, how can I fix it?
0
Comment
Question by:cdemott33
  • 3
4 Comments
 
LVL 22

Accepted Solution

by:
Mrunal earned 500 total points
ID: 40579334
Hi,
Add debug point and check if your code for calculating and assigning column total is getting executed twice or not.

If it is executing only once then before binding grid again just clear it first and then bind again with updated records.
For clearing you can use below code:

GridServices.DataSource = null;
GridServices.Rows.Clear();
GridServices.DataBind();

Hope this solves your issue.
0
 

Author Comment

by:cdemott33
ID: 40580250
Hi Mrunal - Thank you for your suggestion.  I followed your advice and here's what I've found.

Clicking UPDATE caused the RowDataBound event to executing TWICE for some reason?  Any idea as to why that would happen?
0
 

Author Comment

by:cdemott33
ID: 40580283
FYI:  This is my GridView... Just in case you what to have a look.

<asp:GridView ID="GridServices" runat="server" CssClass="mGrid" 
                                    AutoGenerateColumns="False" ShowFooter="True">
                                    <Columns>
                                        <asp:TemplateField HeaderText="Service">
                                            <ItemTemplate>
                                                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" OnCommand="OpenModal" CommandArgument='<%# Eval("ServiceID") %>'><%# Eval("ServiceType") %></asp:LinkButton>
                                            </ItemTemplate>                                    
                                            <HeaderStyle HorizontalAlign="Left" />
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Options">
                                            <ItemTemplate>
                                                <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="false" OnCommand="DeleteService" CommandArgument='<%# Eval("ServiceID") %>' OnClientClick='return confirm("Are you sure you want to delete this service?");'>Delete</asp:LinkButton>
                                            </ItemTemplate>                                    
                                            <HeaderStyle HorizontalAlign="Left" />
                                        </asp:TemplateField>
                                        <asp:BoundField DataField="EstimatedCost" DataFormatString="{0:c}" 
                                            HeaderText="Estimate" >
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle HorizontalAlign="Right" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="ActualCost" DataFormatString="{0:c}" 
                                            HeaderText="Actual" >
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle HorizontalAlign="Right" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="Proposed" DataFormatString="{0:c}" 
                                            HeaderText="Proposed" >
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle HorizontalAlign="Right" ForeColor="#006600" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="Adjusted" DataFormatString="{0:c}" 
                                            HeaderText="Adjusted" >
                                        <HeaderStyle HorizontalAlign="Left" />
                                        <ItemStyle HorizontalAlign="Right" ForeColor="#006600" />
                                        </asp:BoundField>
                                        <asp:BoundField DataField="CreditDebit" DataFormatString="{0:c}"  
                                            HeaderText="CR/DB" >
                                        <HeaderStyle HorizontalAlign="Right" />
                                        <ItemStyle HorizontalAlign="Right" />
                                        </asp:BoundField>
                                    </Columns>
                                    <FooterStyle CssClass="pGrandTotal" />
                                </asp:GridView>

Open in new window

0
 

Author Closing Comment

by:cdemott33
ID: 40580528
Look like I discovered the problem.  There was an onclick="button_event" attached to the button.   I removed the onclick and ... bam... problem solved.  Thanks for your help.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now