Murray Brown
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
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)
ASKER
Hi. Thanks. Would you be able to show me an example of that code
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.DataImageUrlFi eld = "Text1"
oGridView.Columns.Add(bIma geField)
oLoadGrid(oGridView, "Select * From Test2")
Me.Panel_Controls.Controls .Add(oGrid View)
End Sub
Sub oLoadGrid(ByVal oGridView As GridView, ByVal oSQL As String)
Dim cs As String = ConfigurationManager.Conne ctionStrin gs("PSQL") .Connectio nString
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(CommandB ehavior.Cl oseConnect ion)
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
Sub Test
Dim oGridView As New GridView
oGridView.ID = "Control" & CStr(oCount)
oGridView.Style("Width") = "100%"
oGridView.Attributes.Add("
Dim bImageField As New ImageField()
bImageField.DataImageUrlFi
oGridView.Columns.Add(bIma
oLoadGrid(oGridView, "Select * From Test2")
Me.Panel_Controls.Controls
End Sub
Sub oLoadGrid(ByVal oGridView As GridView, ByVal oSQL As String)
Dim cs As String = ConfigurationManager.Conne
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(CommandB
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
ASKER
OK it actually worked. Thanks very much for your help. Greatly appreciated!
you can do it in RowDataBound event ?