Search code examples
asp.netvb.netgridviewobjectdatasourceanonymous-methods

Why won't my anonymous function fire on grid.prerender?


In my gridview I have fields for inserting a new record in the footer.

In my objectdatasource selecting event if no records came back I bind a single mock row to force the footer to show so they can still add records. Since the row does not contain real data I hide the row.

    ...
    If result.ItemCount = 0 Then
        result = mockRow
        AddHandler mygridview.PreRender, AddressOf HideRow
    End If
End Sub

Private Sub HideRow(ByVal sender as Object, ByVal e as EventArgs)
    mygridview.Rows(0).Visible = False
End Sub

This works fine. However, I'd like to condense it like this:

    ...
    If result.ItemCount = 0 Then
        result = mockRow
        AddHandler mygridview.PreRender, Function() mygridview.Rows(0).Visible = False
    End If
End Sub

This compiles fine, but the row doesn't get hidden. Can anyone tell me why my anonymous function isn't getting hit?


Solution

  • The problem is that you are creating a function that returns a boolean instead of assigning a value. If you are using VB 2008 you're stuck, but with VB 2010, you could do a sub instead.

    AddHandler mygridview.PreRender, Sub() mygridview.Rows(0).Visible = False