我正在开发一个学校ERP项目作为ASP.NET Web应用程序,我们在其中有一个gridview。在我的页面上,我想计算gridview的列值之和,但我使用的代码返回特定列总数的两到三倍。关于我们如何计算特定列的正确总数的任何建议?
这里我使用一个复选框列表,其中包含以下项目
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会显示4月16日的费用,当我们选择4月16日,5月16日时,gridview会显示4月16日,5月16日的费用明细。
当我们选择5月16日,6月16日,7月16日,然后gridview显示5月16日,6月16日,7月16日三个费用详情。
我希望输出如下:
现在我想计算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();
}
}
答案 0 :(得分:0)
我确定你同意下面的代码我个人在我的实时项目中尝试这个:
double sum = 0.00;
foreach (DataRow dr in dt.Rows)
{
sum = sum + Convert.ToDouble(dr.ItemArray[6]);
}
txtGrandTotal.Text = sum.ToString();
答案 1 :(得分:-1)
试试这段代码
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();
让我知道这是否适合您