dataGrid_ItemDataBound - System.InvalidCastException

A

AZNewsh

Essentially I am trying to convert a particular column in my datagrid
to an hyperlink on the fly - I have pasted the relevant code and error
below, any advice would be appreciated, I have done several searches
but found nothing that helps.

Code:

Private Sub dataNAV_ItemDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dataNAV.ItemDataBound
Try
Select Case e.Item.ItemType
Case ListItemType.AlternatingItem, ListItemType.Item
Dim drv As DataRowView
drv = CType(e.Item.DataItem, DataRowView)
CType(e.Item.Cells(4).Controls(1), Literal).Text =
_
"<a href='" & drv("C_URL").ToString & "'>" & _
drv("C_URL").ToString & "</a>"
End Select
Catch ex As Exception
error_email.fncErrorHandler(ex)
Label1.Text = ex.ToString
End Try
End Sub

Error:

System.InvalidCastException: Specified cast is not valid. at
spac.update.dataNAV_ItemDataBound(Object sender, DataGridItemEventArgs
e) in c:\inetpub\wwwroot\spac\adminspac\maintain\update.aspx.vb:line
113

{line 113 is: drv = CType(e.Item.DataItem, DataRowView)}

Thanks...
 
S

Scott Mitchell [MVP]

Debug by setting a breakpoint on the line that causes the error. When
it kicks into the debugger, use the Immediate Window to emit the type of
e.Item.DataItem, checking to see if it's a DataRowView or something
else. (Clearly it's not a DataRowView, or you wouldn't be getting the
casting error.)

Essentially I am trying to convert a particular column in my datagrid
to an hyperlink on the fly - I have pasted the relevant code and error
below, any advice would be appreciated, I have done several searches
but found nothing that helps.

Code:

Private Sub dataNAV_ItemDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dataNAV.ItemDataBound
Try
Select Case e.Item.ItemType
Case ListItemType.AlternatingItem, ListItemType.Item
Dim drv As DataRowView
drv = CType(e.Item.DataItem, DataRowView)
CType(e.Item.Cells(4).Controls(1), Literal).Text =
_
"<a href='" & drv("C_URL").ToString & "'>" & _
drv("C_URL").ToString & "</a>"
End Select
Catch ex As Exception
error_email.fncErrorHandler(ex)
Label1.Text = ex.ToString
End Try
End Sub

Error:

System.InvalidCastException: Specified cast is not valid. at
spac.update.dataNAV_ItemDataBound(Object sender, DataGridItemEventArgs
e) in c:\inetpub\wwwroot\spac\adminspac\maintain\update.aspx.vb:line
113

{line 113 is: drv = CType(e.Item.DataItem, DataRowView)}

Thanks...


--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top