I am working on a school ERP project as an ASP.NET web application in which we have a gridview. On my page I want to calculate the sum of column values of the gridview but the code which I am using returns two or three times the total of the particular column. Any suggestions on how we can calculate the right total of the particular column?
Here I use one checkboxlist in which we have the following item
Apr-16,May-16,Jun-16,jul-16,Aug-16,Sep-16,Oct-16,Nov-16,Dec-16,Jan-16,Feb-16,Mar-16
When we select Apr-16 chekbox from cheboxlist then gridview shows Aprl-16 month fee and when we select Apr-16,May-16 then gridview shows Apr-16,May-16 Fees Details.
When we select May-16,Jun-16,Jul-16 then gridview shows May-16,Jun-16,Jul-16 three fees details.
I want the output like:
Now I want to calculate the sum of the particular column in my gridview
Here's my code:
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();
}
}
2条答案
按热度按时间8i9zcol21#
我赶着去办公室
请尝试以下代码
让我知道这是否适合你
ukqbszuj2#
我确信你同意下面的代码,我个人在我的Live Project中尝试过: