Gridview rowdatabound access data items vb

后端 未结 2 1343
花落未央
花落未央 2021-01-25 23:45

I am trying to an ImageUrl to an image in a Template Field in GridView but keep getting the error:

Object reference not set to an instance of an object. on this line:

2条回答
  •  不思量自难忘°
    2021-01-26 00:22

    I think you need to check that its a data row and not the header row

    Try this

    Protected Sub gvImages_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvImages.RowDataBound
    
        If e.Row.RowType = DataControlRowType.DataRow Then
    
            Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)
    
            Dim imagePath As String = rowView("image_path")
    
            Dim strImageUrl As String = "~/admin/images/cases/" & Request.QueryString("uid") & "/" & imagePath
    
            Dim imageFile As System.Web.UI.WebControls.Image = CType(e.Row.FindControl("imageFile"), System.Web.UI.WebControls.Image)
            imageFile.ImageUrl = strImageUrl
    
        End If
    End Sub
    

提交回复
热议问题