private double runningTotal = 0;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("server=Localhost;database=pubs;uid=sa;pwd=1234;");
SqlCommand myCommand = new SqlCommand("SELECT title, price FROM Titles WHERE price > 0", myConnection);
try
{
myConnection.Open();
this.GridView1.DataSource = myCommand.ExecuteReader();
this.GridView1.DataBind();
myConnection.Close();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.ToString());
}
}
private void CalcTotal(string _price)
{
try
{
runningTotal += Double.Parse(_price);
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.ToString());
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CalcTotal(e.Row.Cells[1].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "Total";
e.Row.Cells[1].Text = string.Format("{0:c}",runningTotal);
}
}
}