Datagrid and datareader

B

Bart Schelkens

Hi,

I have 2 more questions :

1. Can I fill a datagrid by using a DataReader or does it have to be a
DataSet or a DataView?

2. In my datagrid I need to display two images and one of does images has te
be a link to a new page. How would you do that ?

Thanks.
 
J

Jared

1:) You can use a datareader to fill a datalist/datagrid.

Dim Conn As New System.Data.SqlClient.SqlConnection
Conn.ConnectionString = "Server=YourServer;
Database=Northwind;Trusted_Connection=True;"

Conn.Open()

Dim cmd As New SqlClient.SqlCommand("SELECT * FROM Employees", Conn)
Me.DataGrid2.DataSource = cmd.ExecuteReader
Me.DataGrid2.DataBind()

Conn.Close()

2:)
If no one else answers your question, before I get home from work I will
throw together an example for you. Is your e-mail address valid?

Jared
 
B

Bart Schelkens

Jared,

thx for the answer.
My email is a valid address (e-mail address removed)
So there's no problem in sending me a reply there.

Thx.
Bart
 
B

Bart Schelkens

Thx for the info
but i can't find the section where they explain how to display images in the
grid.
Nor can i find the section where they tell me how to display an image that
serves as a link for a new page.
 
N

Nick

Part 5 - Template Columns.

Then in the code for the ItemDataBound event you add the HTML for the image.
If you want the image to be a link as well, then add the <A> tags. Eg:

Sub AddImage(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <>
ListItemType.Footer Then
e.Item.Cells(4).Text() = "<img src='../images/picture.gif'></img>"
End If
End Sub

Don't forget you need to add a template column to the datagrid, and set the
OnItemDataBound="AddImage" property for the datagrid.
(For the above code, the template column would need to be the 4th column)

<Columns>
<asp:BoundColumn......></asp:BoundColumn>
<asp:BoundColumn......></asp:BoundColumn>
<asp:BoundColumn......></asp:BoundColumn>
<asp:TemplateColumn HeaderText="No.">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>


Nick
 
B

Bart Schelkens

thx Nick,

I must have looked over it.

Nick said:
Part 5 - Template Columns.

Then in the code for the ItemDataBound event you add the HTML for the image.
If you want the image to be a link as well, then add the <A> tags. Eg:

Sub AddImage(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <>
ListItemType.Footer Then
e.Item.Cells(4).Text() = "<img src='../images/picture.gif'></img>"
End If
End Sub

Don't forget you need to add a template column to the datagrid, and set the
OnItemDataBound="AddImage" property for the datagrid.
(For the above code, the template column would need to be the 4th column)

<Columns>
<asp:BoundColumn......></asp:BoundColumn>
<asp:BoundColumn......></asp:BoundColumn>
<asp:BoundColumn......></asp:BoundColumn>
<asp:TemplateColumn HeaderText="No.">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>


Nick


to
 
B

Bart Schelkens

Nick

in the example I see that the image will always be the same.
I'm trying to find a way (but i'm not very successful) to make the name of
the file variable, because that name is stored in the datasource of my
datagrid.

Thx for any help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top