Solved

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

Posted on 2016-09-21
5
25 Views
Last Modified: 2016-09-21
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

0
Comment
Question by:murbro
  • 3
  • 2
5 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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 ?
0
 

Author Comment

by:murbro
Comment Utility
Hi. Thanks. Would you be able to show me an example of that code
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
Comment Utility
something like this?

Dim bImageField As New ImageField()
        bImageField.DataImageUrlField = "<image url>"

Open in new window


I guess you want to load static image into your gridview?
0
 

Author Comment

by:murbro
Comment Utility
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

1
0
 

Author Closing Comment

by:murbro
Comment Utility
OK it actually worked. Thanks very much for your help. Greatly appreciated!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now