Solved

Gridview Footer Sum Doubles on UPDATE only.

Posted on 2015-01-29
4
331 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
A short film showing how OnPage and Connectwise integration works.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

948 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

18 Experts available now in Live!

Get 1:1 Help Now