• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1706
  • Last Modified:

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
0
kevlause
Asked:
kevlause
  • 2
  • 2
1 Solution
 
chinu1310Commented:
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
0
 
DotNetThinkerCommented:
Here's a method I always use with GridViews. I had to convert it to VB so let me know if you have any problems.

Public Function FindControl(ByVal ControlId As String, ByVal collection As ControlCollection) As Control
            Dim control As Control
            For Each control In collection
                If control.ID = ControlId Then
                    Return control
                End If
 
                If control.HasControls() Then
                    Dim nestedControl As Control =  FindControl(ControlId,control.Controls)
 
                    If Not nestedControl Is Nothing Then
                        Return nestedControl
                    End If
                End If
            Next
            Return Nothing
End Function
 
call it like this:
 
Dim GridView2 As GridView = CType(FnidControl("GridView2",GridView1.Controls), GridView)
0
 
kevlauseAuthor Commented:
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.
0
 
kevlauseAuthor Commented:
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
0
 
DotNetThinkerCommented:
Glad I was able to help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now