4

i.e at the moment I am adding a footer row to my gridview as follows

 Protected Sub gvShoppingCart_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvShoppingCart.RowDataBound ' If we are binding the footer row, let's add in our total If e.Row.RowType = DataControlRowType.Footer Then e.Row.Cells(5).Text = "<strong>Total Cost:</strong>" e.Row.Cells(6).Text = ShoppingCart.Instance.GetSubTotal().ToString("C") End If End Sub 

How can I add more footer rows i.e. Total Discount, Total Saved etc likewise as above

0

3 Answers 3

4

Here's some code for inserting a new row, based on the footer row. You could modify it to insert multiple rows.

 Protected Sub gvShoppingCart_DataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvShoppingCart.DataBound Dim grid as GridView = CType(sender, GridView) ''gets the current footer row to clone Dim footer As GridViewRow = grid.FooterRow Dim numCells = footer.Cells.Count Dim newRow As New GridViewRow(footer.RowIndex + 1, -1, footer.RowType, footer.RowState) ''have to add in the right number of cells ''this also copies any styles over from the original footer For i As Integer = 0 To numCells - 1 Dim emptyCell As New TableCell emptyCell.ApplyStyle(grid.Columns(i).ItemStyle) newRow.Cells.Add(emptyCell) Next newRow.Cells(5).Text = "Total Discount:" newRow.Cells(6).Text = "55.00" ''add new row to the gridview table, at the very bottom CType(grid.Controls(0), Table).Rows.Add(newRow) End Sub 
Sign up to request clarification or add additional context in comments.

1 Comment

Only the solution with creating a new GridViewRow worked for me now in 2022, for a WebForms web app. Not those using the creation of new TableRow that I've tried from other answers on the same subject (multiple GridView footers rows).
1

Using the default control there is only one footer row, therefore, you would have to manage the display of any additional items manually, most likely by inserting
or similar tags to create additional lines.

You could do a custom template for the fields in the footer to help control the layout.

Comments

0

You need to use the <FooterTemplate> in <asp:TemplateField> of the GridView

Here is an example: http://csharpdotnetfreak.blogspot.com/2009/07/display-total-in-gridview-footer.html

A better option would be to use the ListView control. It offers a lot more flexibility for layout.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.