Solved

Gridview Footer Sum Doubles on UPDATE only.

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

13 Experts available now in Live!

Get 1:1 Help Now