Binding Data To A Control Inside A GridVeiw Nested Within A Repeater

I was scratching my head over this issue for a while. I’ve written briefly on the topic of binding a GridView inside of a Repeater before, but I encountered another issue recently when trying to bind data to a within a , nested inside of a .

Originally, I was approaching the task as I would if the GridView wasn’t nested within a Repeater; attempting to reference the data sources object utilizing the GridView’s events arguments variable. I was surprised this didn’t work — when I stepped through the code, I could see that the properties contained the correct data, I just had no means of referencing it. I chalked this behavior up to the GridView being dynamically created inside of a Repeater.

After a bit of research, I found that, in this instance, the events arguments variable needs to be reference through the databinder’s GetPropertyValue method. I’d be lying if I said I fully understood why this works, but regardless here is the code snippet that made it happen:


'Remembering This Method Is Called During A Repeater's Row Databinding Iteration
Protected Sub MyGrid_OnRowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then

    '--- Bind Controls --- |

    Dim MyTextBox As New TextBox
    MyTextBox = e.Row.FindControl("MyTextBoxControl")

    'Code that would work normally, but not while nested (for some reason):
    MyTextBox.Text = e.Row.DataItem("MyFieldValue")

    'Code that executed correctly within the nested Repeater:
    MyTextBox.Text = DataBinder.GetpropertyValue(e.Row.DataItem, "MyFieldValue")

End If

End Sub