Solved

Loop through rows to get control value within nested gridview ASP.NET 2.0

Posted on 2007-11-30
1
7,714 Views
Last Modified: 2013-11-26
Scenario: Gridview with employee, nested gridview with tasks that require completion, an item template within nested gridview that has a dropdown box where the user selects whether the task is completed, in process, not started, etc. An update button in the employee item tempate field (in same cell as nested gridview) that houses the nested gridview. When the update button is clicked, I want to iterate through the nested gridview and get the new value of the dropdownlist and send it to the database.

 Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim gvInside As GridView = CType(FindControl("gvTasks"), GridView)
        Dim iStatus As Integer
        Dim StatusBox As DropDownList
        For Each row As GridViewRow In gvInside.Rows
            StatusBox = CType(FindControl("ddCompletionStatus"), DropDownList)
            iStatus = StatusBox.SelectedValue
        Next
    End Sub
The code above generates the following error: System.NullReferenceException was unhandled by user code   Message="Object reference not set to an instance of an object."

Any help is appreciated. Thank you!
 
0
Comment
Question by:cmcormick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 96

Accepted Solution

by:
Bob Learned earned 125 total points
ID: 20382269
You need to use FindControl with the Row.  You would need to find out what the current row for the outer grid is, and then get a reference to the inside GridView.

One possibility:




Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim gvInside As GridView = CType(gvOutSide.Rows(0).FindControl("gvTasks"), GridView)
        Dim iStatus As Integer
        
        For Each row As GridViewRow In gvInside.Rows
            Dim StatusBox As DropDownList = CType(row.FindControl("ddCompletionStatus"), DropDownList)
            iStatus = StatusBox.SelectedValue
        Next
    End Sub

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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