Showing posts with label Total value of Gridview in Footer. Show all posts
Showing posts with label Total value of Gridview in Footer. Show all posts

Thursday, May 21, 2009

Total value of Gridview in Footer

//Get the two float values for two Gridview Columns and initialize it


float total1 = 0,total2=0;



//On page load Bind the Employee Salary Function with DataGrid


protected void Page_Load(object sender, EventArgs e)

{

DataGrid1.DataSource = GetEmployeeSalary();

DataGrid1.DataBind();

}

//Getting the data from Database


public DataSet GetEmployeeSalary()

{

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString.ToString());

string sqlcmd = "SELECT Name, Bonus, SUM(Salary) as Salary"+

" FROM CustomerSalary"+

" group by Name, Bonus";

SqlCommand cmd = new SqlCommand(sqlcmd, conn);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds);

return ds;

}

//Using Item Data Bound to get the total value in footer of Gridview


protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)

{

if (e.Item.ItemType!=ListItemType.Header && e.Item.ItemType!=ListItemType.Footer)

{

total1 += float.Parse(e.Item.Cells[1].Text);

total2 += float.Parse(e.Item.Cells[2].Text);

e.Item.ForeColor = System.Drawing.Color.Blue;

}

else if (e.Item.ItemType == ListItemType.Footer)

{

e.Item.Cells[0].Text = "Total";

e.Item.Cells[1].Text = total1.ToString();

e.Item.Cells[2].Text = total2.ToString();

//e.Item.Cells[3].Text = total.ToString();

}


}