nested gridview findcontrol not working

Posted on 2007-07-31
Last Modified: 2012-06-22
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
            End If
            qtyshippedLabel.Text = count
            count = 0
Question by:kevlause
    LVL 14

    Expert Comment

    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
    LVL 12

    Accepted Solution

    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
                Return Nothing
    End Function
    call it like this:
    Dim GridView2 As GridView = CType(FnidControl("GridView2",GridView1.Controls), GridView)

    Author Comment

    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.

    Author Comment

    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
                    For Each row As GridViewRow In GridView2.Rows
                        count = count + row.Cells(0).Text
                End If
                qtyshippedLabel.Text = count
            End If
        End Sub
    LVL 12

    Expert Comment

    Glad I was able to help

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now