c# - 如何使用 C# .net 计算 gridview 中特定列值的总和

标签 c# .net sql-server

我正在从事一个学校 ERP 项目,该项目是一个 ASP.NET Web 应用程序,其中我们有一个 GridView 。在我的页面上,我想计算 gridview 的列值总和,但我使用的代码返回特定列总和的两倍或三倍。关于我们如何计算特定列的正确总数有什么建议吗?

enter image description here

这里我使用一个复选框列表,其中有以下项目

Apr-16,May-16,Jun-16,jul-16,Aug-16,Sep-16,Oct-16,Nov-16,Dec-16,Jan-16,Feb-16,Mar-16

当我们从 cheboxlist 中选择 Apr-16 chekbox 时,gridview 会显示 Aprl-16 的月费,当我们选择 Apr-16、May-16 时,gridview 会显示 Apr-16、May-16 的费用明细。

当我们选择 May-16、Jun-16、Jul-16 时,gridview 会显示 May-16、Jun-16、Jul-16 三个费用明细。

我想要这样的输出:

enter image description here

现在我想计算我的 gridview 中特定列的总和

这是我的代码:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace egurkul
{
    [Serializable]
    public class field
    {
        public string duemonth { get; set; }
        public decimal Admission_Fee { get; set; }
        public decimal Prospectus_Registration_Fee { get; set; }
        public decimal Maintenance_Fee { get; set; }
        public decimal Computer_Fee { get; set; }
        public decimal Examination_Fee { get; set; }
        public decimal Tution_Fee { get; set; }
    }

    public partial class feeRecipt : System.Web.UI.Page
    {
        SqlCommand cmd;
        SqlConnection con;
        string duemonth;
        int i;

        decimal TotalAdmission_Fee, TotalProspectus_Registration_Fee, TotalMaintenance_Fee, TotalComputer_Fee, TotalExamination_Fee, TotalTution_Fee;
        decimal Admission_Fee, Prospectus_Registration_Fee, Maintenance_Fee, Computer_Fee, Examination_Fee, Tution_Fee;

        DataTable dt;
        List<field> data = null; 
        decimal Total = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getddlclass();
                bindchbxlstduedate();
            }
        }

        public void getddlclass()
        {
             SqlConnection con = new SqlConnection();
             con.ConnectionString = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;

             con.Open();
             SqlDataAdapter da = new SqlDataAdapter("select *from class", con);
             DataSet ds = new DataSet();
             da.Fill(ds);

             ddlclassname.DataSource = ds;
             ddlclassname.DataTextField = "class_name";
             ddlclassname.DataValueField = "class_id";
             ddlclassname.DataBind();
             ddlclassname.Items.Insert(0, new ListItem("--Select--", "0"));

             con.Close();
        }

        public void bindchbxlstduedate()
        {
            using (SqlConnection conn = new SqlConnection())
            {
                 con = new SqlConnection();
                 con.ConnectionString = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;

                 con.Open();
                 SqlCommand cmd = new SqlCommand();
                 cmd.Connection = con;

                 cmd.CommandText = "Readduedat";
                 cmd.CommandType = CommandType.StoredProcedure;

                 using (SqlDataReader sdr = cmd.ExecuteReader())
                 {
                     while (sdr.Read())
                     {
                         ListItem item = new ListItem();
                         item.Text = sdr["duemonth"].ToString();
                         item.Value = sdr["duedateid"].ToString();
                         chbxlstduedate.Items.Add(item);
                     }
                 }

                 con.Close();
           }
     }

     protected void chbxlstduedate_SelectedIndexChanged(object sender, EventArgs e)
     {
         for (i = 0; i < chbxlstduedate.Items.Count; i++)
         {
            if (chbxlstduedate.Items[i].Selected)
            {
                duemonth = Convert.ToString(chbxlstduedate.Items[i]);
                SqlConnection con = new SqlConnection();
                con.ConnectionString = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
                con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "readfeeshead";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@duemonth", duemonth);
                cmd.Parameters.AddWithValue("@classname", Convert.ToString(ddlclassname.SelectedItem));
                cmd.Parameters.AddWithValue("@feecatename", Convert.ToString(ddlstcategory.SelectedItem));
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                       duemonth1 = sdr["duemonth"].ToString();
                       Admission_Fee1 = sdr["Admission_Fee"].ToString();
                       Prospectus_Registration_Fee1 = sdr["Prospectus_Registration_Fee"].ToString();
                       Maintenance_Fee1 = sdr["Maintenance_Fee"].ToString();
                       Computer_Fee1 = sdr["Computer_Fee"].ToString();
                       Examination_Fee1 =sdr["Examination_Fee"].ToString();
                       Tution_Fee1 = sdr["Tution_Fee"].ToString();

                    }
                }

                con.Close();

                if (data == null)
                    data = new List<field>();

                field f1 = new field();

                f1.duemonth = duemonth1;

                if (Admission_Fee1 == "")
                {
                    f1.Admission_Fee = Convert.ToDecimal(0);
                }
                else
                {
                   f1.Admission_Fee = Convert.ToDecimal(Admission_Fee1);
                }

                if (Prospectus_Registration_Fee1 == "")
                    {
                        f1.Prospectus_Registration_Fee = Convert.ToDecimal(0);
                    }
                    else
                    {
                        f1.Prospectus_Registration_Fee = Convert.ToDecimal(Prospectus_Registration_Fee1);
                    }
                    if (Maintenance_Fee1 == "")
                    {
                        f1.Maintenance_Fee = Convert.ToDecimal(0);
                    }
                    else
                    {
                        f1.Maintenance_Fee = Convert.ToDecimal(Maintenance_Fee1);
                    }
                    if (Computer_Fee1 == "")
                    {
                        f1.Computer_Fee = Convert.ToDecimal(0);
                    }
                    else
                    {
                        f1.Computer_Fee = Convert.ToDecimal(Computer_Fee1);
                    }
                    if (Examination_Fee1 == "")
                    {
                        f1.Examination_Fee = Convert.ToDecimal(0);
                    }
                    else
                    {
                        f1.Examination_Fee = Convert.ToDecimal(Examination_Fee1);
                    }
                    if (Tution_Fee1 == "")
                    {
                        f1.Tution_Fee = Convert.ToDecimal(0);
                    }
                    else
                    {
                        f1.Tution_Fee = Convert.ToDecimal(Tution_Fee1);
                    }
                    data.Add(f1);
                    ViewState["_data"] = data;
                    GridView1.DataSource = data;
                    GridView1.DataBind();

            }


        }

    }



 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView dtview = e.Row.DataItem as DataRowView;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            Label lbladmissionfee = (Label)e.Row.FindControl("lbladmissionfee");
            TotalAdmission_Fee = TotalAdmission_Fee + Convert.ToDecimal(lbladmissionfee.Text);

            Label lblcomputerfee = (Label)e.Row.FindControl("lblcomputerfee");
            TotalComputer_Fee = TotalComputer_Fee + Convert.ToDecimal(lblcomputerfee.Text);

            Label lblExaminationfee = (Label)e.Row.FindControl("lblExaminationfee");
            TotalExamination_Fee = TotalExamination_Fee + Convert.ToDecimal(lblExaminationfee.Text);

            Label lblMaintenancefee = (Label)e.Row.FindControl("lblMaintenancefee");
            TotalMaintenance_Fee = TotalMaintenance_Fee + Convert.ToDecimal(lblMaintenancefee.Text);

            Label lblProsepectusRegistrationFee = (Label)e.Row.FindControl("lblProsepectusRegistrationFee");
            TotalProspectus_Registration_Fee = TotalProspectus_Registration_Fee +Convert.ToDecimal(lblProsepectusRegistrationFee.Text);

            Label lblTutionfee = (Label)e.Row.FindControl("lblTutionfee");
            TotalTution_Fee = TotalTution_Fee + Convert.ToDecimal(lblTutionfee.Text);


        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {

            Label lbl_totaladmissionfee = (Label)e.Row.FindControl("lbl_totaladmissionfee");
            lbl_totaladmissionfee.Text = TotalAdmission_Fee.ToString();

            Label lbl_totalcomputerfee = (Label)e.Row.FindControl("lbl_totalcomputerfee");
            lbl_totalcomputerfee.Text = TotalComputer_Fee.ToString();

            Label lbl_Totalexaminationfee = (Label)e.Row.FindControl("lbl_Totalexaminationfee");
            lbl_Totalexaminationfee.Text = TotalExamination_Fee.ToString();

            Label lbl_totalmaintenancefee = (Label)e.Row.FindControl("lbl_totalmaintenancefee");
            lbl_totalmaintenancefee.Text = TotalMaintenance_Fee.ToString();

            Label lbl_TotalProsepectusRegistrationFee = (Label)e.Row.FindControl("lbl_TotalProsepectusRegistrationFee");
            lbl_TotalProsepectusRegistrationFee.Text = TotalProspectus_Registration_Fee.ToString();

            Label lbl_Totaltutionfee = (Label)e.Row.FindControl("lbl_Totaltutionfee");
            lbl_Totaltutionfee.Text = TotalTution_Fee.ToString();



            decimal alltotal = TotalTution_Fee + TotalProspectus_Registration_Fee + TotalMaintenance_Fee + TotalExamination_Fee + TotalComputer_Fee + TotalAdmission_Fee;
            Label lbl_Totalsamount = (Label)e.Row.FindControl("lbl_Totalsamount");
            lbl_Totalsamount.Text = alltotal.ToString();



        }


    }

最佳答案

嗯,我赶时间去办公室

试试这段代码

int sum = 0;
for (int i = 0; i < dvData.Rows.Count; ++i)
{
    sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value);
}
label.text = sum.ToString();

告诉我这是否适合你

关于c# - 如何使用 C# .net 计算 gridview 中特定列值的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35924241/

相关文章:

c# - 字符串到字典,然后再返回

c# - PBKDF2 在 .NET 中使用 SHA 256

sql - 选择没有唯一键的单行

sql - 聚集超前/滞后

c# - 为什么基本身份验证无法与我的 Java SOAP Web 服务 WCF 客户端一起使用?

c# - 无法从C#中的相同类但使用不同方法访问对象属性

c# - Windows Phone 中的 SqlConnection

.net - 如何调试 .NET 应用程序间接导致的 BSOD 崩溃

sql-server - Azure SQL Server : There are multiple execution plans created if the Stored Procedure Option (Keep Plan) is set

c# - 从循环 C# 添加到数组