Solved

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

Posted on 2016-09-21
5
27 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
ID: 41808212
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
ID: 41808216
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
ID: 41808262
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
ID: 41808417
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
ID: 41808420
OK it actually worked. Thanks very much for your help. Greatly appreciated!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

18 Experts available now in Live!

Get 1:1 Help Now