Sum of the values in gridview

Nageswari
Posted by Nageswari under C# category on | Points: 40 | Views : 3821
Hi, I am trying to find the value for the sum of gridview but am not getting it
Here is my code anybody help me.......


protected void BtnSubmit_Click(object sender, EventArgs e)
{


OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["cons"].ConnectionString);

if (DDlDepartment.SelectedValue != "null")
{

con.Open();
string str = DDlDepartment.SelectedItem.Text;
OleDbDataAdapter da = new OleDbDataAdapter("select * from salary where department='" + str + "'", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

}
else
{
string str1 = DDLStaffid.SelectedItem.Text;

OleDbDataAdapter da1 = new OleDbDataAdapter("select staffId,stafftype,department,staffname from salary where staffId='"+str1+"'", con);
con.Open();
DataSet ds1 = new DataSet();
da1.Fill(ds1);
GridView1.DataSource = ds1;
GridView1.DataBind();
con.Close();
}
}

decimal totalBasicPay = 0;
decimal totalAllowance = 0;
decimal totalGrossPay = 0;
decimal totalDeduction = 0;
decimal totalNetPay = 0;
int totalItems = 0;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblBasicPay = (Label)e.Row.FindControl("lblBasicPay");
Label lblAllowance = (Label)e.Row.FindControl("lblAllowance");
Label lblGrossPay = (Label)e.Row.FindControl("lblGrossPay");
Label lblDeduction = (Label)e.Row.FindControl("lblDeduction");
Label lblNetPay = (Label)e.Row.FindControl("lblNetPay");

decimal BasicPay = Decimal.Parse(lblBasicPay.Text);
decimal Allowance = Decimal.Parse(lblAllowance.Text);
decimal GrossPay = Decimal.Parse(lblGrossPay.Text);
decimal Deduction = Decimal.Parse(lblDeduction.Text);
decimal NetPay = Decimal.Parse(lblNetPay.Text);


totalBasicPay += BasicPay;
totalAllowance += Allowance;
totalGrossPay += GrossPay;
totalDeduction += Deduction;
totalNetPay += NetPay;

totalItems += 1;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbltotalBasicPay = (Label)e.Row.FindControl("lbltotalBasicPay");
Label lbltotalAllowance = (Label)e.Row.FindControl("lbltotalAllowance");
Label lbltotalGrossPay = (Label)e.Row.FindControl("lbltotalGrossPay");
Label lbltotalDeduction = (Label)e.Row.FindControl("lbltotalDeduction");
Label lbltotalNetPay = (Label)e.Row.FindControl("lbltotalNetPay");


lbltotalBasicPay.Text = totalBasicPay.ToString();
lbltotalAllowance.Text = totalAllowance.ToString();
lbltotalGrossPay.Text = totalGrossPay.ToString();
lbltotalDeduction.Text = totalDeduction.ToString();
lbltotalNetPay.Text = totalNetPay.ToString();


Label5.Text = (totalNetPay / totalItems).ToString("F");
}

}

Comments or Responses

Posted by: Shankul2784 on: 8/16/2010 Level:Bronze | Status: [Member] | Points: 10

Hi,

there is no error or not wrong statements. Please check with your data.
Posted by: Nageswari on: 8/19/2010 Level:Starter | Status: [Member] | Points: 10
Thanks a lot......Its Working now........................
Posted by: Santosh0683 on: 8/24/2010 Level:Starter | Status: [Member] | Points: 10
thanks it is nice code

Login to post response