计算总和并在c#中显示在网格上 [英] calulate sum and show on grid in c#

查看:63
本文介绍了计算总和并在c#中显示在网格上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想逐行计算总和并在网格视图中打印标签上的总和你可以帮我看看如何在标签上显示总和而不使用c中绑定的行数据#

我的代码是



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

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>

推荐答案

您的代码是mes sy所以我不能为你修改它。获得总和的方法是遍历网格中的项目并将它们相加。



例如



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

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();


这篇关于计算总和并在c#中显示在网格上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆