Solved

Gridview Footer Sum Doubles on UPDATE only.

Posted on 2015-01-29
4
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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