i have a datagridview which are used to display a transaction record. And one of the columns in datagridview is \"amount\"
girdview codeenter code here
try this...
int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);
}
GridViewRow row = GridView1.FooterRow;
((Label)row.FindControl("lbltotal")).Text=sum.ToString();
use table.Compute function
private void ComputeBySalesSalesID(DataSet dataSet)
{
// Presumes a DataTable named "Orders" that has a column named "Total."
DataTable table;
table = dataSet.Tables["Orders"];
// Declare an object variable.
object sumObject;
sumObject = table.Compute("Sum(Total)", "EmpID = 5");
//Find label
GridViewRow footer = dgOpenBal.FooterRow;
var lblTotal = (Label)footer.FindControl("lblTotal");
lblTotal.Text = sumObject.ToString();
}
Another approach use stored procedure to get calculated amount from Sproc
private void ComputeBySalesSalesID(DataSet ds)
{
if (ds.Tables[0].Rows.Count > 0)
{
DataRow drSum = ds.Tables[0].Rows[0];
GridViewRow footer = dgOpenBal.FooterRow;
var lblTotal = (Label)footer.FindControl("lblTotal");
lblTotal.Text = drSum["sum"].ToString();
}
}