locked
asp.net C# GridView run twice. Duplicate Sum results RRS feed

  • Question

  • User-1116300467 posted

    I tried to set sums on footer in gridview. I had succeded this but if I add a small piece of code it duplicates the results. For exapmple instead of 2 it appears 4. It seems that run twice.

    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Default2 : System.Web.UI.Page
    {
        int[] sum_nmb = new int[30] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int count_col = 0;
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated)
            {
    
            }
            else
            {
                Response.Redirect("~/LoginPage.aspx");
            }
            date_textbox.Text = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Label10.Text = date_textbox.Text;
            Label13.Text = date_textbox.Text;
        }
    
         protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType== DataControlRowType.DataRow)
            {
                count_col = 0;
                foreach (TableCell cell in e.Row.Cells)
                {
                    try
                    {
                        sum_nmb[count_col] = sum_nmb[count_col] + Int32.Parse(e.Row.Cells[count_col].Text);
                    }
                    catch
                    {
                        sum_nmb[count_col] = sum_nmb[count_col] + 0;
                    }
                    count_col = count_col + 1;
                }
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                for(int i = 0; i < GridView1.Columns.Count; i++)
                {
                    if (sum_nmb[i] != 0)
                    {
                        e.Row.Cells[i].Text = sum_nmb[i].ToString();
                    }
                    else if (sum_nmb[i]==0 && i > 0)//if I remove this "else if" block it works like a charm
                    {//
                        GridView1.Columns[i].Visible = false;//
                    }//
    
                }
                e.Row.Cells[0].Text = "Σύνολο:";
                e.Row.Cells[27].Text = " ";
            }
        }
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" ForeColor="#333333" AllowSorting="True" ShowFooter="True" CssClass="auto-style8" GridLines="Vertical" Font-Size="Medium" Font-Strikeout="False" OnRowDataBound="gridview1_RowDataBound">

    So if I insert this else if (sum_nmb[i]==0 && i > 0) { GridView1.Columns[i].Visible = false; } and any piece of code it run 2 times.</div> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0"> I had read that maybe is a bug but I couldn't figure it out. Thnx in advance

    </div> </div> </div>

    Tuesday, June 9, 2020 7:49 AM

All replies

  • User475983607 posted

    My best guess is you added a join in the query that fetches the data source.  The code is not running twice.  A change to the query returns double the records.  

    Tuesday, June 9, 2020 12:08 PM
  • User-1116300467 posted

    But this happen when  I add same code inside the for loop in footer block. sql query is running well. i checked it. thnx

    Tuesday, June 9, 2020 12:11 PM