dataGrid_ItemDataBound - System.InvalidCastException

Discussion in 'ASP .Net Datagrid Control' started by AZNewsh, Nov 17, 2004.

  1. AZNewsh

    AZNewsh Guest

    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...
     
    AZNewsh, Nov 17, 2004
    #1
    1. Advertising

  2. 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.)


    AZNewsh wrote:
    > 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

    http://www.4GuysFromRolla.com

    * When you think ASP.NET, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], Nov 18, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Floela
    Replies:
    3
    Views:
    582
    Jon Skeet
    Jul 10, 2003
  2. Arjen
    Replies:
    1
    Views:
    7,783
    Carl Prothman [MVP]
    Aug 6, 2003
  3. =?Utf-8?B?cm9kY2hhcg==?=

    datagrid_ItemDataBound question

    =?Utf-8?B?cm9kY2hhcg==?=, Nov 23, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    443
    =?Utf-8?B?cm9kY2hhcg==?=
    Nov 24, 2004
  4. =?Utf-8?B?cm9kY2hhcg==?=

    DataGrid_ItemDataBound

    =?Utf-8?B?cm9kY2hhcg==?=, Sep 9, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    791
    Nathan Sokalski
    Sep 10, 2005
  5. Teemu Keiski
    Replies:
    0
    Views:
    498
    Teemu Keiski
    Jan 2, 2006
Loading...

Share This Page