Problem with conversion from C# to VB.NET

P

PatLaf

Hello to all, I converted a response I recieved in this newsgroup last week to vb.net but I can't convert this sub correctly. Can anyone help....the dgDeftects.ItemCommand never fires and I think it's because it's not wired up correctly.




Private Sub InitializeComponent()
dgDefects.ItemCommand += New System.Web.UI.WebControls.DataGridCommandEventHandler(AddressOf dgDefects_ItemCommand)

Me.Load += New System.EventHandler(Me.Page_Load)
End Sub 'InitializeComponent

Private Sub dgDefects_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)

Dim ovalue As Integer

ovalue = Integer.Parse(CType(e.Item.Controls(2), TableCell).Text)

If e.CommandName = "Increase" Then
CType(e.Item.Controls(2), TableCell).Text = (ovalue + 1).ToString()
Else
CType(e.Item.Controls(2), TableCell).Text = (ovalue - 1).ToString()
End If
End Sub 'dgDefects_ItemCommand

If Not Page.IsPostBack Then
Dim dt As New DataTable("MyTable")

dt.Columns.Add("IntValue", System.Type.GetType("System.Int16"))
dt.Columns.Add("StrValue", System.Type.GetType("System.String"))

Dim dr As DataRow = dt.NewRow()

dr(0) = 1

dt.Rows.Add(dr)

dr = dt.NewRow()

dr(0) = 2

dt.Rows.Add(dr)

dgDefects.DataSource = dt

dgDefects.DataBind()
End If
End Sub 'Page_Load

this is all the code that I have but the ide is complaining about the initialize component method.
Thanks in advance,
Patrick Laferriere
 
P

PatLaf

Errrr.....duh....I figured it out.....

it's this simple I thin

AddHandler dgDefects.ItemCommand, AddressOf dgDefects_ItemComman

that got the command event to fire and it seems to work...

thanks
 
J

Jeffrey Tan[MSFT]

Hi Patrick,

Yes, C# use += operator to add event handler for object, in VB.net the
alternative is Addhanlder keyword.
Also, you should use AddressOf keyword to retrieve function's address.

I am glad you figure it out yourself, if you have further concern, please
feel free to post, I will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
F

Felbrigg

If your converting to VB the problem is most likely that the
InitialiseComponent method code show here is not needed. If the dgDefects
object is declared "WithEvents" you should need to manually wire the events
shown here.

PatLaf said:
Hello to all, I converted a response I recieved in this newsgroup last
week to vb.net but I can't convert this sub correctly. Can anyone
help....the dgDeftects.ItemCommand never fires and I think it's because it's
not wired up correctly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top