0

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?

adam0101
  • 29,096
  • 21
  • 96
  • 174
  • Consider using the Listview control. It renders an insert column for you. http://msdn.microsoft.com/en-us/library/bb515103.aspx Scroll to bottom to see figure. – Raj Kaimal Apr 23 '10 at 19:54

3 Answers3

2

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
  • 6,095
  • 1
  • 21
  • 22
0

don't you need to add the parameters?

AddHandler mygridview.PreRender, _ 
   Function(sender as Object, e as EventArgs) mygridview.Rows(0).Visible = False
Glennular
  • 17,827
  • 9
  • 58
  • 77
0

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
  • 112
  • 1
  • 11