views:

608

answers:

3

I have seen several tutorials on how to achieve this.

However in my opinion they require a lot of prior knowledge on how to programatically refer to each item.

Does anyone have a link to or can create a relatively basic example of how to achive a running total in the footer for an ASP:Gridview?

+2  A: 

This is what I use:

protected void InvoiceGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    var invoice = (Invoice) e.Row.DataItem;
    if (e.Row.RowType == DataControlRowType.Header)
    {
     totalAmt = 0;
    }
    else if (e.Row.RowType == DataControlRowType.DataRow)
    {
     totalAmt += invoice.Amount;
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
     var amountTotalLabel = (TextBox) e.Row.FindControl("AmountTotalTextBox");
     amountTotalLabel.Text = totalAmt.ToString("0.00");
    }
}

TotalAmt is protected instance variable on the page. Not sure if it's what you were looking for based on your comment about "programmatic knowledge." But it works and is fairly straight-forward. The gridview is bound to a List<Invoice> in this case.

JohnOpincar
A: 

Add the footer Template and on the RowDataBound, have a global variable to store the summation sum,

At the e.Row.RowType = DataControlRowType.DataRow type do the summation , and @ the e.Row.RowType = DataControlRowType.Footer store the vale in the appropriate cell

for further info look @ MSDN LINK

novice
A: 

Hi, i want to add textbox value in gridview columns and then add total columns and then finally show in gridview footer textbox. need ur help.

if (e.Row.RowType == DataControlRowType.DataRow) {

         TextBox TextBox1 = (TextBox)e.Row.FindControl("txtsubtotal");
           total += Convert.ToInt32(TextBox1.Text);
       }

       else if (e.Row.RowType == DataControlRowType.Footer)
       {

           TextBox TextBox2 = (TextBox)e.Row.FindControl("total");
           TextBox2.Text = total.ToString();

       } 

Above is my code.i need ur help. its urgent.Thanks in advance

Dev