Ayman
Ayman

Reputation: 872

Multiple footer rows in gridview

How can i display information in multiple rows in grid-view footer using code behind

for example

header1   header2           header3

 data        data              data

 data        data              data

             Amount:           value

             Shipping Cost:    value

             Total Amount:     value

How can i add Shipping cost and Total Amount in the footer

protected void gridinvoice_RowDataBound(object sender, GridViewRowEventArgs e)
{
    decimal Amount = 0;

    for (int y = 0; y < gridinvoice.Rows.Count; y++)
    {
        Amount += Convert.ToDecimal(gridinvoice.Rows[y].Cells[4].Text);
    }

    if (e.Row.RowType == DataControlRowType.Footer)
    {  
        e.Row.Cells[3].Text = "Amount";
        e.Row.Cells[4].Text = Amount.ToString();
    }

}

Upvotes: 0

Views: 3568

Answers (1)

Rojalin Sahoo
Rojalin Sahoo

Reputation: 1025

As per your requirement you could try like this:

protected void gridinvoice_RowDataBound(object sender, GridViewRowEventArgs e)
{
    decimal Amount = 0;

    for (int y = 0; y < gridinvoice.Rows.Count; y++)
    {
        Amount += Convert.ToDecimal(gridinvoice.Rows[y].Cells[4].Text);
    }

    if (e.Row.RowType == DataControlRowType.Footer)
    {  
        e.Row.Cells[3].Text = "Amount: <br/> Shipping Cost: <br/> Total Amount:";
        e.Row.Cells[4].Text = Amount.ToString() +"<br/>"+shipping.ToString() +"<br/>" + total.ToString();
    }

}

Or you could Structure your footer on Aspx page using Controls and footer template and assign the value to the controls on code behind.

  protected void gridinvoice_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        decimal Amount = 0;

        for (int y = 0; y < gridinvoice.Rows.Count; y++)
        {
            Amount += Convert.ToDecimal(gridinvoice.Rows[y].Cells[4].Text);
        }

        if (e.Row.RowType == DataControlRowType.Footer)
        {  
           Label lblamount = e.Row.FindControl("lblid") as Label;
           lblam.Text = Amount.ToString();
        }

    }

Upvotes: 1

Related Questions