Question

I am converting some classes from C# to VB.NET and this line can't be converted properly. I tried few online converters but they don't seem to work.

this.mGrid.RowDataBound += new GridViewRowEventHandler(RowDataBoundHandler);

Converted VB.NET that does not work.

Me.mGrid.RowDataBound += New GridViewRowEventHandler(RowDataBoundHandler)

The following are the two errors I am getting from that. Can anyone help me out coz my brain is dead now.

Thanks,

Error 4 Delegate 'System.Web.UI.WebControls.GridViewRowEventHandler' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor. C:\My Projects\PMS\App_Code\GridViewHelper.vb 110 62 C:\My Projects\PMS\

Error 3 'Public Event RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event. C:\My Projects\PMS\App_Code\GridViewHelper.vb 110 9 C:\My Projects\PMS\

Was it helpful?

Solution 2

Try using AddHandler if you want to bind the event manually:

AddHandler Me.mGrid.RowDataBound, AddressOf RowDataBoundHandler

Or you can bind the event in the ASPX markup:

<asp:GridView ... RowDataBound="RowDataBoundHandler" runat="server" />

Or use Tim Schmelter's approach.

OTHER TIPS

You don't need to register the event handler programmatically in VB.NET. You can either use the aspx markup to declare the event handler(OnRowDataBound = "RowDataBoundHandler") or use the Handles clause. You can even use one for multiple controls, separate them by commas.

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) _
    Handles GridView1.RowDataBound
End Sub

If you want to add it manually anyway, use AddHandler:

AddHandler Me.GridView1.RowDataBound, AddressOf RowDataBoundHandler

Use this instead, which is the VB way of doing this. VB uses AddHandler to identify the event, and the AddressOf points to the delegate.

AddHandler mGrid.RowDataBound, AddressOf RowDataBoundHandler
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top