Click here to Skip to main content
15,903,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All Friends good morning

I have inter some row in DataGrid View in C# GUI. Can you Please help how sum of the column of the grid at the last of grid view . (Like Footer)..


Thanks in advance..
Posted

When the GridView is bound to a data source control, the GridView is composed by enumerating the DataSource and building one row at a time. As each GridView row is databound, the GridView's RowDataBound event is fired. This provides page developers an opportunity to tap into the GridView creation process at row-level granularity.

C#
int quantityTotal = 0;
void detailsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        quantityTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
          "Quantity"));
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.Cells[0].Text = "Totals:";
        e.Row.Cells[1].Text = quantityTotal.ToString("d");
        e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
        e.Row.Font.Bold = true;
    }
}
 
Share this answer
 
Comments
psgviscom 13-Mar-12 0:11am    
my 5!
Hi, try this:
C#
int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
    sum += Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value);
}
label1.text = sum.ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900