Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.net VB.net Adding images to a progrematically added GridView

Hi
I am using the following code to add a GridView to my ASP.net webform panel. I am trying to create a column that shows images
from my resources. If I add a GridView manually I use the DataImageUrlField property to specify where the image is, but I don't know how to do this programatically

                       Dim oGridView As New GridView
                        oGridView.ID = "Control" & CStr(oCount)
                        oGridView.Style("Width") = "100%"
                        'NOTE: PREVENT ENTER BUTTON TRIGGERING btnSave_Click EVENT
                        oGridView.Attributes.Add("onkeydown", "return (event.keyCode!=13);")

                        Dim bfield As New BoundField()
                        bfield.HeaderText = "Name"
                        bfield.DataField = "Name"
                        oGridView.Columns.Add(bfield)

Open in new window

Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

If I add a GridView manually I use the DataImageUrlField property to specify where the image is, but I don't know how to do this programatically

you can do it in RowDataBound event ?
Avatar of Murray Brown

ASKER

Hi. Thanks. Would you be able to show me an example of that code
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi. Here is my code. At the bottom I show the results in the GridView. cloud1.jpg is an image in my resources

Sub Test
                        Dim oGridView As New GridView
                        oGridView.ID = "Control" & CStr(oCount)
                        oGridView.Style("Width") = "100%"
                        oGridView.Attributes.Add("onkeydown", "return (event.keyCode!=13);")

                        Dim bImageField As New ImageField()
                        bImageField.DataImageUrlField = "Text1"
                        oGridView.Columns.Add(bImageField)

                        oLoadGrid(oGridView, "Select * From Test2")

                        Me.Panel_Controls.Controls.Add(oGridView)
End Sub

    Sub oLoadGrid(ByVal oGridView As GridView, ByVal oSQL As String)

        Dim cs As String = ConfigurationManager.ConnectionStrings("PSQL").ConnectionString
        Dim cn As New SqlConnection(cs)

        Try

            Dim cmd As New SqlCommand(oSQL, cn)

            '// open the connection
            cn.Open()

            '// execute the sql statement
            Using reader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

                oGridView.DataSource = reader
                oGridView.DataBind()

            End Using

        Catch ex As Exception
            Response.Write(ex.Message & " yuma310")
        Finally
            If cn.State <> ConnectionState.Closed Then
                cn.Close()
            End If
        End Try
    End Sub

User generated image
OK it actually worked. Thanks very much for your help. Greatly appreciated!