help with Nested Gridview footer totals

fullyii

Member
Joined
Aug 19, 2005
Messages
22
Location
Chicago
Programming Experience
1-3
I have a nested gridview and would like to know the best way to display the column summary data in the footer of the nested gridview.I am using template column to display the nested gridview and the following code to show the nested records within the master gridview. The parameters are within the sql datasourse.

I would like to place the sum of "INV_RECPT_AMT" into the footer of the nested gridview "GridViewInvoice" label "lblTotal"


Thank you for your help!
Code Behind

VB.NET:
Protected Sub GridViewProject_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
Handles GridViewProject.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim mySDS As SqlDataSource = CType(e.Row.FindControl("SqlDataSourceInvoiceChild"), SqlDataSource)
            mySDS.SelectParameters(0).DefaultValue = e.Row.DataItem("ELPM_NO")
            Dim mySDSInvoice As SqlDataSource = CType(e.Row.FindControl("SqlDataSourceInvoiceNo"), SqlDataSource)
            mySDSInvoice.SelectParameters(0).DefaultValue = e.Row.DataItem("ELPM_NO")
        End If        
    End Sub

Untitled.png
 

Attachments

  • aspx page.txt
    7.5 KB · Views: 27
Last edited:
My eyes!!!! I didn't read your code to see if you're putting the total in a seperate gridview but the theory is the same.

In the row databound event You'll want to make a check if the row is a datarow.

VB.NET:
If e.Row.RowType = DataControlRowType.DataRow Then...

Once you've determined that it's a datarow you can pull the value from the cell and add it to a variable holding the running total.

VB.NET:
myTotal += CDbl(e.Row.Cells(0).Text)

The last row you're going to loop through is the footer row so you can assign your total value to a cell in that row.

VB.NET:
ElseIf e.Row.RowType = DataControlRowType.Footer Then
    e.Row.Cells(0).Text = "Grand Total: " & myTotal.ToString()
End If
 
I am not sure where to place the code so I created a rowbound event for the nested gridview and I am not seeing the results in the footer.

Here is what I have in the code behind

VB.NET:
    Protected Sub GridViewInvoice_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow Then
            'add total receipt
            totalreceipt += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "INV_RECPT_AMT"))
        ElseIf e.Row.RowType = DataControlRowType.Footer Then
            e.Row.Cells(0).Text = "Totals"
            e.Row.Cells(1).Text = totalreceipt.ToString("c")

        End If

    End Sub
 
Have you stepped through the code to make sure your values are getting added? Seems like you're missing Handles GridViewInvoice.RowDataBound. Might just be an error with copying to the fourm.

It could be as simple as setting the showfooter property to true.

VB.NET:
<asp:GridView ID=".." ShowFooter=True />
 
Last edited:
when I add the hadles GridviewInvoice.RowDataBound I get an error that Handles clause requires with events variable.. That is why I left it out.

Also since the grid is nested it is not included in the intell-sense

I stepped through and the gridviewinvoice rowdatabound never gets touched.

Yes I do have the footer visible as well.
 
when I add the hadles GridviewInvoice.RowDataBound I get an error that Handles clause requires with events variable.. That is why I left it out.

Dragging a gridview to your page should set the WithEvents automatically.

VB.NET:
Protected WithEvents GridViewInvoice As System.Web.UI.WebControls.GridView

I set up a form and added your If statement to the RowDataBound event of a GridView and was able to see the results in the footer data.

I would suggest highlighting your GridViewInvoice control and double clicking on RowDataBound from the events page of properties to have Visual Studio wire up the event for you.
 
Were you able to see the footer details in the nested grid?

I moved the following code to to the Parent gridview("GridViewProject") and I was able to see the title text in the parent footer.

VB.NET:
 Protected Sub GridViewProject_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewProject.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim mySDS As SqlDataSource = CType(e.Row.FindControl("SqlDataSourceInvoiceChild"), SqlDataSource)
            mySDS.SelectParameters(0).DefaultValue = e.Row.DataItem("ELPM_NO")

            Dim mySDSInvoice As SqlDataSource = CType(e.Row.FindControl("SqlDataSourceInvoiceNo"), SqlDataSource)
            mySDSInvoice.SelectParameters(0).DefaultValue = e.Row.DataItem("ELPM_NO")

        ElseIf e.Row.RowType = DataControlRowType.Footer Then
            e.Row.Cells(5).Text = "Totals"        
        End If
    End Sub

The nested gridview I am unable to write to the footer because I can't find the nested control. Since the gridview is nested it does not allow me to add the handles with either.

Thank you for your patience and help! I would really like to fuigure this one out.
 
The nested gridview I am unable to write to the footer because I can't find the nested control. Since the gridview is nested it does not allow me to add the handles with either

You should be able to select the gridview from the Edit Templates menu. From there you can get to the events. You are correct that the handles isn't valid in the nested gridview.

VB.NET:
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow Then
            'add total receipt
            totalreceipt += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "service_charge"))
        ElseIf e.Row.RowType = DataControlRowType.Footer Then
            e.Row.Cells(0).Text = "Totals"
            e.Row.Cells(1).Text = totalreceipt.ToString("c")
            totalreceipt = 0
        End If
    End Sub

The only change I needed to make was to reset my total variable after setting the footer so it starts from 0 on the next line of the parent gridview.

EditTemplates.jpg

Child Grid.jpg
 

Latest posts

Back
Top