Solved

Gridview Footer Sum Doubles on UPDATE only.

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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