Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to calculate sum row by row and print the sum on label in grid view can you please help me how to show sum on label without using row data bound in c#
my code is

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ERPOrganization.DAL;
using System.Data;
using ERPLedger.Entities;
using System.Globalization;
using ERPLedger.BAL;

namespace ERPOrganization
{
    public partial class WebForm2 : System.Web.UI.Page
    {     
        decimal crdTot = 0;
        decimal debt = 0;
        Label lblTot;
        string list = "";
        decimal yTotal = 0;
        decimal yClBal = 0;
        decimal total = 0;
        decimal bal = 0;
        string to = "";
        clsLgrEnty objEnt = new clsLgrEnty();
        clslgrBAL objBal = new clslgrBAL();

       protected void Page_Load(object sender, EventArgs e)
        {       
           to=txtToDate.Text;  
            txtCode.Visible = true;
            lblSelect.Visible = true;                
        }
   
        [System.Web.Script.Services.ScriptMethod()]
        [System.Web.Services.WebMethod]
        public static List<string> GetPartyCode(string prefixText)
        {
          
            string str = "Select top 20  pcode+ '  / ' +PName+'-'+ptype as PartyName from ACPMST where   pcode like '" + prefixText + "%' order by pcode";
           
           
            DataTable dt = UtilityClass.GetData(str, "str_temp");

            List<string> PCodes = new List<string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                PCodes.Add(dt.Rows[i]["PartyName"].ToString());
            }
            return PCodes;

       
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {

            //showNoSummaryGrid();
            //showYesSummaryGrid();
           
        }

     //Shows Ledger Yes Summmary 
        public void showYesSummaryGrid()
          {

              if (DropDownList1.SelectedIndex == 2)
              {
                  lblType.Text = "S";

              }
              else if (DropDownList1.SelectedIndex == 3)
              {
                  lblType.Text = "C";
              }

            
            string[] StrArry = null;
            
           DateTime frm = Convert.ToDateTime(txtFrom.Text);
            string strFrm=frm.ToShortDateString();
            DateTime to = Convert.ToDateTime(txtToDate.Text);
           string strTo = to.ToShortDateString();
            string pcode = txtCode.Text;
            StrArry = pcode.Split('/');
           string code = StrArry[0];
           txtCode.Text = code;
           objEnt.VrdateFrm1 = Convert.ToDateTime(strFrm) ;
           objEnt.VrdateTo = Convert.ToDateTime(to);
            objEnt.Pcode = txtCode.Text;
            objEnt.PType = lblType.Text;

           DataTable objdt = objBal.YesOpBalSummary(objEnt);
           if (objdt.DataSet.Tables["str_temp"].Rows.Count != 0)
           {
               YesLedgerGrid.DataSource = objdt;
               YesLedgerGrid.DataBind();
               ledgerGrid.Visible = false;
           }
           else
           {
               lblData.Text = "No Records Found";
           }
       
       }

       //Show Ledger No Summary

        public void showNoSummaryGrid()
        {
            if (DropDownList1.SelectedIndex == 2)
            {
                lblType.Text = "S";

            }
            else if (DropDownList1.SelectedIndex == 3)
            {
                lblType.Text = "C";
            }

            string[] StrArry = null;
            string[] strCodeArr = null;

            DateTime frm = Convert.ToDateTime(txtFrom.Text);
            string strFrm = frm.ToShortDateString();
            DateTime to = Convert.ToDateTime(txtToDate.Text);
            string strTo = to.ToShortDateString();

            //to  get pcode  split from '/'
            string pcode = txtCode.Text;
            StrArry = pcode.Split('/');
            string code = StrArry[0];
            txtCode.Text = code;

            //to get individual pcode split from','
            string strcode = txtCode.Text;
            string concateStr = "";

            concateStr = "'" + strcode.Replace(",", "'',''") + "'";
            concateStr = concateStr.Replace(" ", "");

            objEnt.VrdateFrm1 = Convert.ToDateTime(strFrm);
            objEnt.VrdateTo = Convert.ToDateTime(to);
            objEnt.Pcode = concateStr;
            objEnt.PType = lblType.Text;
            string strpCode = "";


            DataTable objdtNo = objBal.NoSummary(objEnt);
            if (objdtNo.DataSet.Tables["str_temp"].Rows.Count != 0)
            {
                for (int i = 0; i < objdtNo.Rows.Count; i++)
                {
                    DataRow dr = objdtNo.Rows[i];

                    if (strpCode != dr["Pcode"].ToString())
                    {
                        Label lblTot = (Label)e.Row.FindControl("lblTotal");
                        lblTot.Text = debt.ToString();
                        debt = 0;

                    }
                    strpCode = dr["pcode"].ToString();
                    debt = debt + Convert.ToDecimal(dr["Debit"]);

                }
                ledgerGrid.DataSource = objdtNo;
                ledgerGrid.DataBind();
                YesLedgerGrid.Visible = false;
            }
            else
            {
                lblData.Text = "No Records Found";
            }
           
        }

        protected void txtToDate_TextChanged(object sender, EventArgs e)
        {
          
        }

        protected void txtCode_TextChanged(object sender, EventArgs e)
        {
            string[] arrlist = null;
            string pcode = txtCode.Text;
           arrlist = pcode.Split('/');
            string code = arrlist[0];
            txtCode.Text = code;
            //ListBox1.Items.Add(txtCode.Text);
            //ListBox1.Visible = true;
        }       
        protected void ledgerGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            ledgerGrid.PageIndex = e.NewPageIndex;
            ledgerGrid.DataBind();
        }
        protected void YesLedgerGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
         {
          YesLedgerGrid.PageIndex = e.NewPageIndex;
           YesLedgerGrid.DataBind();
        }
        protected void ledgerGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            decimal strcr=0;
            decimal cr = 0;
          
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
               
                // decimal rowTotal = Convert.ToDecimal (DataBinder.Eval(e.Row.DataItem, "Debit"));
                //total = total + rowTotal;

                decimal rowCrd = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Credit"));
              
                crdTot = crdTot + rowCrd;

                decimal rowBal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "clBAl"));

                if (rowBal <= 0)
                {
                    Label lblCrDr = (Label)e.Row.FindControl("lblCrDr");
                    lblCrDr.Text = "Cr";
                }
                else
                {
                    Label lblCrDr = (Label)e.Row.FindControl("lblCrDr");
                    lblCrDr.Text = "Dr";
                }
            strcr = Convert.ToDecimal(Math.Abs(string.IsNullOrEmpty(rowBal.ToString()) ? Convert.ToDouble("0.0") : Convert.ToDouble(rowBal)));
            Label newBal = (Label)e.Row.FindControl("lblNewBal");
            newBal.Text = Convert.ToString(strcr);
                bal = total - crdTot;
                lblTot = (Label)e.Row.FindControl("lblTotal");
                lblTot.Text = debt.ToString();      
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                 //Label lblbAL = (Label)e.Row.FindControl("lblBalance");
                //lblbAL.Text = bal.ToString();

                //Label lblCrd = (Label)e.Row.FindControl("lblcredit");
                //lblCrd.Text = crdTot.ToString();
               // int cr = Convert.ToInt32(strcr.ToString());
            }
        }

        protected void YesLedgerGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
               decimal rowTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "debit"));
               yTotal = yTotal + rowTotal;

                decimal rowBal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "clBAl"));
                yClBal = yClBal + rowBal;               
            }
           if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lblTot = (Label)e.Row.FindControl("lblTotal");
               lblTot.Text = yTotal.ToString();

                Label lblBal = (Label)e.Row.FindControl("lblBalance");
                lblBal.Text = yClBal.ToString();

            }
        }

        protected void ddSummery_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddSummery.SelectedIndex == 1)
            {
                ledgerGrid.Visible = false;
                YesLedgerGrid.Visible = true;
                showYesSummaryGrid();
               
            }
            else if (ddSummery.SelectedIndex == 2)
            {
                YesLedgerGrid.Visible = false;
                ledgerGrid.Visible = true;
                showNoSummaryGrid();          
           }
        }

        protected void ledgerGrid_RowCreated(object sender, GridViewRowEventArgs e)
        {
           
        }

        protected void ledgerGrid_SelectedIndexChanged(object sender, EventArgs e)
        {


        }

        //protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        //{



        //    int position = 0;

        //    for (byte i = 0; i < ListBox1.Items.Count; i++)
        //    {
        //        position = ListBox1.SelectedIndex;
        //    }

        //    ListBox1.Items.RemoveAt(position);
        //}    

    }
}</string></string></string>
Posted
Updated 8-Nov-13 22:26pm
v2
Comments
OriginalGriff 9-Nov-13 4:24am    
Do you seriously think I - or anyone else - wants to wade through you entire file looking to see what may or may not be relevant? Particularly if you can't even be bothered to try and make it readable? No. Not going to happen.
Use the "Improve question" widget to edit your question. Remove the commented out code. Remove anything which isn't relevant to the specific problem. Then enclose the rest of the code in pre tags, using the "Code" dropdown list above the textbox.
Then, explain what you have tried, and where you are stuck.
And we might look at it...

1 solution

Your code is messy so i cant modify it for you. The way to get the sum is to loop through the items in the grid and sum them.

for example

C#
int ItemToSum = 0;
    for (int i = 0; i < GridView1.Rows.Count; i++ )
       {
               ItemToSum += Convert.ToInt32( GridView1.Rows[i].Cells[2].Text.ToString());
        }
  label1.Text = ItemToSum.ToString();
 
Share this answer
 
Comments
Joezer BH 10-Nov-13 5:11am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900