To display an icon in a datagrid

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 7
Reputation: deeraj is an unknown quantity at this point 
Solved Threads: 0
deeraj's Avatar
deeraj deeraj is offline Offline
Newbie Poster

To display an icon in a datagrid

 
0
  #1
Mar 17th, 2005
Hi I have a dataset which acts as a datasource to my datagrid. It just shows all the columns from a table. One of the columns is an image column. How do i show these icons\pictures on the grid?

My code looks like this:
dim cmd as sqlcommand
dim da as sqldataadapter
dim ds as new dataset

cmd.commandtext = "select * from tbl_auths"
cmd.connection = cn
'assuming connection (cn) to be open
da = New SqlDataAdapter(cmd)
da.Fill(ds,"tbl_auths")
dgrauth.datasource = ds.tables("tbl_auths")
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 27
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: To display an icon in a datagrid

 
0
  #2
Apr 7th, 2005
I will provide the link:

LINK

Originally Posted by Bipin Joshi
Displaying Images from SQL Server database in ASP.NET DataGrid

Introduction

ASP.NET DataGrid web control can be bound quickly with any database table like SQL server. For most of the data types all you need to do is to add a bound column and set its datafield property to the column name from the table. However, displaying image data type i.e. binary data is not that easy. It requires some kind of extra coding from developer's side to make that work. In this article we will see how to do just that. SQL Server tables

The articles make use of a SQL server database table called images. The structure of which can be as follows:
  1. CREATE TABLE [dbo].[IMAGES] (
  2. [imgid] [int] IDENTITY (1, 1) NOT NULL ,
  3. [imgdata] [image] NULL
  4. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

Creating a web form with DataGrid

In order to proceed with the example create a new ASP.NET web form say webform1.aspx and add a DataGrid web control to it. Add two columns to it :

  • one bound column with datafield set to imgid and
  • another templated column with an image web control.
In the Page_Load event of the web form bind the datagrid with the database.
  1. If Not Page.IsPostBack Then
  2. BindGrid()
  3. End If
  4. Public Sub BindGrid()
  5. Dim connstr As String =
  6. "Integrated Security=SSPI;
  7. Initial Catalog=Northwind;
  8. Data Source=SERVER\netsdk"
  9. Dim cnn As New SqlConnection(connstr)
  10. Dim da As New SqlDataAdapter("select * from images", cnn)
  11. Dim ds As New DataSet()
  12. da.Fill(ds, "images")
  13. DataGrid1.DataSource = ds
  14. DataGrid1.DataBind()
  15. End Sub


Now write the ItemDataBound event for the datagrid as follows:

  1. Private Sub DataGrid1_ItemDataBound
  2. (ByVal sender As Object,
  3. ByVal e As DataGridItemEventArgs)
  4. Handles DataGrid1.ItemDataBound
  5.  
  6. Dim img As System.Web.UI.WebControls.Image
  7. If e.Item.ItemType = ListItemType.AlternatingItem Or
  8. e.Item.ItemType = ListItemType.Item Then
  9. img = CType(e.Item.Cells(1).Controls(1),
  10. System.Web.UI.WebControls.Image)
  11. img.ImageUrl =
  12. "webform2.aspx?id=" & e.Item.Cells(0).Text
  13. End If
  14. End Sub
This event gets fired for all the rows from the DataGrid just before data binding action. All the code from the event handler should be familiar to you except the line where we are setting ImageUrl property of the image web control. Generally this property is set to a valid image file (gif/jpg etc.) but here, we are setting it to another web form. What does that means? This means that the web form (webform2.aspx) is returning the image data based on some processing logic. That is why we are also passing image id to the web form via query string. Now, let us examine the code from the webform2.aspx. Creating web form that returns image data

The webform2.aspx simply contains following code in the Page_Load event handler.
  1. Dim connstr As String = "Integrated Security=SSPI;
  2. Initial Catalog=Northwind;Data Source=SERVER\netsdk"
  3. Dim cnn As New SqlConnection(connstr)
  4. Dim cmd As New SqlCommand
  5. ("select * from images where id="
  6. & Request.QueryString("id"), cnn)
  7. cnn.Open()
  8. Dim dr As SqlDataReader = cmd.ExecuteReader()
  9. dr.Read()
  10. Dim bindata() As Byte = dr.GetValue(1)
  11. Response.BinaryWrite(bindata)
Here, we are retrieving the image based on the query string passed. Note how we have used Response.BinaryWrite to emit the binary image data. Now, run the application and you should get all the images displayed in columns of the DataGrid. Summary

In this article we saw how to retrieve image data type from SQL server database and display the image in DataGrid web control. We saw here that ImageUrl of the image web control can be set to another web form that emits binary image data via Response.BinaryWrite method.
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 7
Reputation: deeraj is an unknown quantity at this point 
Solved Threads: 0
deeraj's Avatar
deeraj deeraj is offline Offline
Newbie Poster

Re: To display an icon in a datagrid

 
0
  #3
Apr 8th, 2005
Thanks for that piece of code. But Im afraid Im not looking for this....because my project is in VB.NET and not ASP.NET....there is no "itemdatabound" event for datagrid in VB.NET. Please let me know if there is any other way....
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 27
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: To display an icon in a datagrid

 
0
  #4
Apr 8th, 2005
Sorry about that...I was not paying attention when I posted that from work.

Here is a link to some sample code that may be useful.

LINK

Hope this helps
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 7
Reputation: deeraj is an unknown quantity at this point 
Solved Threads: 0
deeraj's Avatar
deeraj deeraj is offline Offline
Newbie Poster

Re: To display an icon in a datagrid

 
0
  #5
Apr 8th, 2005
Thanks for that too...but again this is not what I am lookin for! He is displayin a picture from the app path. I want it from an SQL table.... Any other ideas?!
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 27
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: To display an icon in a datagrid

 
0
  #6
Apr 8th, 2005
OK, point taken.

Here is some further information that I hope will be of more help : LINK

LINK 2

snippets:

  1. byte[] m_bynewblob = (byte[])myDateSet.Tables["myTable"].Rows[m_iRowID]["Photo"];

This is a perplexing little problem, and I plan to solve it for you...... weekend project.

I will let you know what I figure out....if anything.
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 7
Reputation: deeraj is an unknown quantity at this point 
Solved Threads: 0
deeraj's Avatar
deeraj deeraj is offline Offline
Newbie Poster

Re: To display an icon in a datagrid

 
0
  #7
Apr 11th, 2005
Somehow its not working. I worked on it today, I also used some other concepts like stream and memorystream...but all in vain. The image in the table is not loaded from any physical path. It is an image column having some value like '0x151C2F00020000000D000E0014..........'. It is essentialy small round images having a single color. I donno how the did it because the person who did it is now not part of the project. When I save it to some location using the code provided in the link, I am not able to open those images!

The project was in MS Access and is working fine still. I am trying to convert it into VB.NET and all these problems are happening! In my datagrid its showing as "byte[]array" where as in MS Access, the proper image is loaded.

Its great to know that you are interested in helping me. Thanks!! Looking forward to more help from you.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC