Search   Articles   Dev Forums   Personalize   Favorites   Member Login   ASP.Net Hosting
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Sunday, February 19, 2006

Search Directory
Trusted by over 7 million customers!
ASP.NET
VB.Net
C-Sharp
SQL Server
 
More Partners >>

How to Display Image in GridView Control Using Property

Author: DevASP Team
Download Source Code : 692_PropertyGrid.zip

In this article I will try to explain you how you can display images using property.

To begin with this article, create new application in ASP.Net 2.0. Add a GridView control and bind the Image control using ImageUrl attribute with database. The code for the .aspx page is as follows:

 

<asp:GridView ID="GridView1" runat="server">

    <Columns>

     <asp:TemplateField HeaderText="Ticket Reserved">

      <ItemTemplate>

       <asp:Image ID="imgButton" runat="server" ImageUrl='<%# GetImage(Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "TicketReserved"))) %>' ></asp:Image>

        </ItemTemplate>          

       </asp:TemplateField>           

     </Columns>

  </asp:GridView>

 

Now on .cs page add the following property:

 

public string GetImage(bool allowed)

{

     if (allowed)

         return "~/Images/image1.gif";

     else

         return "~/Images/image2.gif";   

}

 

Now on the page load bind the GridView control with database:

 

protected void Page_Load(object sender, EventArgs e)

{

 

SqlConnection objConn = new SqlConnection("Server=SERVER-981CB1DBE;Persist Security Info=False;User ID=userid;Password=password;Initial Catalog=TempDatabase");

 

objConn.Open();

 

SqlCommand objCmd = new SqlCommand("Select TicketReserverd From Ticket", objConn);

objCmd.CommandType = CommandType.Text;

objCmd.ExecuteNonQuery();

 

objConn.Close();

 

}

 

 











Article Comments
Name:  virender singh 
defind in databse TicketReserverd ( image,null)



Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.


Name:  sudheer 
this is very useful to me,but i have one more doubt,you are just talking about 2 images only but i have number of images in my database how to dispaly the all the images within gridview control.

plz send me resultcode as early as possible.
tahnk you

Name:  Tousif 
how to show image from sql server-2000 to grid view in asp.net, please help me, this is urgent for my project. i m waiting 4 ur quick answer, plz co-operate me. thank u


Name:  arief 
hi,
im arief,i want display image to gridview
using sql server 2005 & asp.net,c#

Name:  sachin 
'GetImage' is a 'type' but is used like a 'variable'

i have got an above error during compilation.

Name:  jenny 
used this "ticket reserved" field as bit not as image

TicketReserved(bit, null)

handle values at front end

Name:  Faraz 
sachin i think you are using it as varible here GetImage is a method which is accessed on front end through server delimiters that is '<%# %>' try to use it like this and in code behind check that you have added the method

Name:  Vanitha 
It's Wonderful. But how to display different images from sql which is in image datatype.

Name:  Ramu 
please send me How to bind images from a table stored in sql 2005 to a gridview

Name:  ravi 
i have made a folder named image in project and there is some jpg image in that. now i want to display image in gridview



Disclaimer - Privacy
Copyright © 2008 DevASP.net