locked
Grand Total For Each Column Based On RefNo RRS feed

  • Question

  • User-807418713 posted

    Hello

    This is my Gridview Data

    RefNo Item Color Quantity Rate Percentage
    R1 I1 A 150 5 10
    R2 I1 B 200 5 10
    R3 I4 C 50 6 11
    R3 I4 D 50 7 20
    R3 I4 E 50 5 10
               
        Total 400    

    Column total should come based on Refno

    That is 150 + 200 +50  =400

    coz 50 three times but see only one Refno

    how to get column total

    Thanking You

    Thursday, December 5, 2019 10:39 AM

Answers

  • User288213138 posted

    Hi Gopi.MCA,

    Column total should come based on Refno

    That is 150 + 200 +50  =400

    coz 50 three times but see only one Refno

    According to your description, i made demo for you. I compare the 2 adjacent values by traversing the GridView, and if they are not the same, let them add.

    The code:

     
     <asp:GridView ID="GridView1" runat="server" OnDataBound="GridView1_DataBound" ShowFooter="true"></asp:GridView>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[6] { new DataColumn("RefNo"), new DataColumn("Item"), new DataColumn("Color"), new DataColumn("Quantity"), new DataColumn("Rate"), new DataColumn("Percentage") });
                    dt.Rows.Add("R1", "I1", "A", 150, 5, 10);
                    dt.Rows.Add("R2", "I1", "B", 200, 5, 10);
                    dt.Rows.Add("R3", "I4", "C", 50, 6, 11);
                    dt.Rows.Add("R3", "I4", "D", 50, 7, 20);
                    dt.Rows.Add("R3", "I4", "E", 50, 5, 10);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
    
                }
            }
           
            protected void GridView1_DataBound(object sender, EventArgs e)
            {
                double sumqty = Convert.ToDouble(GridView1.Rows[0].Cells[3].Text);
                for (int rowIndex = 0; rowIndex < GridView1.Rows.Count - 1; rowIndex++)
                {
    
                    GridViewRow gvRow = GridView1.Rows[rowIndex];
                    GridViewRow gvPreviousRow = GridView1.Rows[rowIndex + 1];
    
                    if (gvRow.Cells[0].Text != gvPreviousRow.Cells[0].Text)
                    {
                        sumqty += Convert.ToDouble(GridView1.Rows[rowIndex + 1].Cells[3].Text);
                    }
                    GridView1.FooterRow.Cells[2].Text = "Total";
                    GridView1.FooterRow.Cells[3].Text = sumqty.ToString();
    
                }
    
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 6, 2019 3:27 AM