Displaying Images in a GridView
By AzamSharp
Views: 8001

Introduction:

I have been receiving a lot of emails asking how can I display an image into one of the column of the GridView control. In this article I will show you that how easy it is to display the images into the GridView column.

Setting up the Database Table:

Check out the image below to have a better idea of the database table.

As, you can see that I have use the relative path instead of the actual physical path of the images. All the images are stored inside the Images directory which is contained inside the application virtual directory. Hence, the path of the images might look something like /MyApplication/Images/Image1.jpg.

Populating the GridView Control:

The next step is to populate the GridView control with data as well as images. Take a look at the code below which is used to populate the GridView.

private void BindData()

{

SqlConnection myConnection = new SqlConnection(ConnectionString);

SqlDataAdapter ad = new SqlDataAdapter("SELECT UserID, FirstName, LastName,Url FROM Users", myConnection);

DataSet ds = new DataSet();

ad.Fill(ds);

GridView1.DataSource = ds;

GridView1.DataBind();

}

As, you can see the above code is pretty straight forward and you might have done this thousand of times. Now, let's see how we can display the images into the GridView control.

Display Images into the GridView Control:

The first thing you need to do is to add a template column in the GridView control. Once, you have added the template column simply add an Image server control inside the template column. The HTML code of the GridView will look something like the following code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4">

<Columns>

<asp:BoundField DataField="UserID" HeaderText="UserID" />

<asp:BoundField DataField="FirstName" HeaderText="First Name" />

<asp:TemplateField HeaderText="Image">

<ItemTemplate>

<asp:Image ID="Image1" ImageUrl='<%# (string) FormatImageUrl( (string) Eval("Url")) %>' runat="server" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />

<RowStyle BackColor="White" ForeColor="#330099" />

<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />

<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />

<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />

</asp:GridView>

 

I have made the Image tag bold in the above code so you will easily identify it. Next thing we need to see is the purpose of the FormatImageUrl method which is used to assign the correct url to the ImageUrl property of the Image control.

protected string FormatImageUrl(string url)

{

if (url != null && url.Length > 0)

return ("~/" + url);

else return null;

}

The purpose of "~/" is to map the url relative to the root. This means that the Image control will look for the ImageUrl starting from the root of the website.

Now, if you run your application provided that there are images in the directory which you are using you will see image column in the GridView control just like shown below:

 

Please feel free to download the the source code at the end of this article.

I hope you liked the article! happy coding.

 

 

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: thank you
Name: duc_clo
Date: 2/3/2007 9:28:33 AM
Comment:
thank you very much , i have big trouble with this problem , thank you , i'm VietNamemes
Subject: Source Code
Name: Ms N
Date: 4/10/2007 4:26:45 PM
Comment:
Hi Azam,im still having difficulty trying to display the images.Is the source code still available for download?
Subject: RE: Source Code
Name: AzamSharp
Date: 4/12/2007 1:07:22 PM
Comment:
What problems are you facing?
Subject: Dynamic Images
Name: Shamira
Date: 4/19/2007 2:44:28 AM
Comment:
hi Azam your code is great & helped me a lot.
but still i got 1 more problem.
that's how can we display an image according to the value in a field ?
for example lets say in my table i got a field called "Status"
1 - ok.jpg
2 - error.jpg

how can we display these images according to the value ?
please respond soon.
you can use the email address
Subject: ASP.NET
Name: Amit
Date: 6/15/2007 2:25:41 AM
Comment:
It's too good..
Subject: images stars like gmail
Name: fahad
Date: 8/15/2007 5:24:22 AM
Comment:
hello, i am a fan of your articles and love them all will you please write an article about displaying images such as the starts in gmail in a grid to know the emails you want to read later or something like that.
thanks alot
Subject: thank u
Name: kemal
Date: 8/16/2007 7:48:51 AM
Comment:
thanks a lot ist helpfull
Subject: code not working if image format is different
Name: ajay
Date: 10/17/2007 6:09:01 AM
Comment:
hi

ur code doesn' t show images when
image format is different

with regards

ajay
Subject: good
Name: Lasantha Peththawadu
Date: 2/21/2008 11:48:19 PM
Comment:
I offer my great thanks.I have problem with this point.
Subject: Problem
Name: uttam kumar singh
Date: 3/29/2008 4:30:27 AM
Comment:
I have some problem. I want to display all character in UPPER CASE in a data bound field.Please help me.
Thanks............!
Subject: RE: Problem
Name: AzamSharp
Date: 3/31/2008 9:43:19 AM
Comment:
Hi uttam kumar singh, You need to set the style of of the databound fields to use the following: text-transform:uppercase;
Subject: found error
Name: kruti
Date: 4/1/2008 3:37:23 AM
Comment:
this code is used by me. and i found the error.
error is --- Compiler Error Message: BC30109: 'String' is a class type and cannot be used as an expression.
Subject: Source code
Name: surya sunil
Date: 4/17/2008 10:51:05 AM
Comment:
can you provide source to this article please.I was facing some diffculties to display image.
I was very much immpresed by your article.
Subject: RE: Source Code
Name: AzamSharp
Date: 4/17/2008 8:30:02 PM
Comment:
Hi Surya Sunil, What problems are you facing?



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008