Link to home
Start Free TrialLog in
Avatar of kevlause
kevlause

asked on

nested gridview findcontrol not working

I have a nested gridview that I need to sum up the values of a cell in and set a label in the parent gridview based on that sum. the below code I think should do that but gridview2 always comes back as nothing. What am I missing? Why can't I use findcontrol to find gridview2?

I tried the code in gridview1.databound and in page load and still nothing...

        Dim count As Integer = 0
        Dim GridView2 As GridView
        Dim qtyshippedLabel As Label
        For Each row As GridViewRow In GridView1.Rows
            qtyshippedLabel = row.FindControl("qtyshippedLabel")
            GridView2 = row.FindControl("GridView2")
            If GridView2 IsNot Nothing Then
                For Each r As GridViewRow In GridView2.Rows
                    count = count + r.Cells(0).Text
                Next
            End If
            qtyshippedLabel.Text = count
            count = 0
        Next
Avatar of chinu1310
chinu1310
Flag of United States of America image

First you will have to use find control method for the second Gridview

like say
GridView gv = gridview1.FindControl("Grdiview2);
Than gv.Findcontrol("Sumlabel");

Than assign value to this label.

Hope it helps
ASKER CERTIFIED SOLUTION
Avatar of DotNetThinker
DotNetThinker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kevlause
kevlause

ASKER

That function gives me the same problem. :-(

Also Chinu1310 the label is part of gridview 1. And I'm doing a findcontrol on Gridview2 for each row of gridview1 as that is where it is nested.
Got it!!! It didn't databind so there were no rows in gridview2 yet! You get the points DNT!!


Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim count As Integer = 0
            Dim qtyshippedLabel As Label = e.Row.FindControl("qtyshippedLabel")
            Dim GridView2 As GridView = CType(e.Row.FindControl("GridView2"), GridView)
            If GridView2 IsNot Nothing Then
                GridView2.DataBind()
                For Each row As GridViewRow In GridView2.Rows
                    count = count + row.Cells(0).Text
                Next
            End If
            qtyshippedLabel.Text = count
        End If
    End Sub
Glad I was able to help