views:

54

answers:

3

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?

A: 

don't you need to add the parameters?

AddHandler mygridview.PreRender, _ 
   Function(sender as Object, e as EventArgs) mygridview.Rows(0).Visible = False
Glennular
It must be able to infer them. I tried it both ways. Still didn't work.
adam0101
+1  A: 

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
Gideon Engelberth
A: 

This is what you need:

AddHandler mygridview.PreRender, Sub(sender as Object, e as EventArgs) mygridview.Rows(0).Visible = False

So, you were both half right.

Bryan