Solved

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

Posted on 2016-09-21
5
31 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 50

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 50

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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