Click here to Skip to main content
15,905,068 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I need to load images in the gridview dynamically, I use the below code through which I'm unable to load the images in the gridview. Kindly let me know which went wrong in this code.

Please find the code below:

The code in Default.aspx file

<form id="form1" runat="server">

<asp:GridView ID="gvImages" runat="server" AutoGenerateColumns="False"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="white">
<asp:BoundField HeaderText = "Image Name" DataField="imagename" />
<asp:TemplateField HeaderText="Image">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImageID") %>' Height="150px" Width="150px"/>

The code in Default.aspx.cs file

protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
    /// <summary>
    /// function is used to bind gridview
    /// </summary>
    public void BindGridData()
        DataTable dt = new DataTable();

        DataRow dr = dt.NewRow();
        dr["imagename"] = "Image1";
        dr["ImageID"] = @"D:\Mathi_Working\01816_Backup\Mathi\Images\9.gif";

        DataRow dr1 = dt.NewRow();
        dr1["imagename"] = "Image2";
        dr1["ImageID"] = @"D:\Mathi_Working\01816_Backup\Mathi\Images\HereSmiley.JPG";

        gvImages.DataSource = dt;
        gvImages.Attributes.Add("bordercolor", "black");

Kindly let me know where I'm making mistake.

Thanks & Regards,

The below code solved my problem

string rootImgPath = ResolveUrl("~/Mathi/Images/");
Share this answer
You can do the same in Grid View RowBound event also... find the attached code snippet

protected void grd_listOutsourceRequest_RowDataBound(object sender, GridViewRowEventArgs e)

           if (e.Row.RowType == DataControlRowType.DataRow)
               Image img = new Image();

             img.ImageUrl = "../Images/Blue.png";
                       img.Visible = true;
                       e.Row.Cells[6].Text = "Open";


Share this answer
Mathi2code 30-Jul-13 8:52am    
tried giving like this
dr["ImageID"] = "../Images/9.gif";
still not working
but Previously nothing was displaying now I'm getting a default image icon which has been broken
Do not give Image path like this is the absolute path and will not work.

dr1["ImageID"] = @"D:\Mathi_Working\01816_Backup\Mathi\Images\HereSmiley.JPG";

This should be a like...

dr1["ImageID"] = @"http://localhost:53734/Mathi/Images/HereSmiley.JPG";
Share this answer
Mathi2code 30-Jul-13 8:55am    
tried like this dr1["ImageID"] = @"http://localhost:1311/Images/HereSmiley.JPG"; the image is not loading...
Previously nothing was displaying now I'm getting a default image icon which has been broken
idenizeni 30-Jul-13 18:52pm    
Review the HTML source to ensure you are referencing the correct path to your images directory. Right click the default image in your browser and check the path... is it the correct path to your image?
Mathi2code 30-Jul-13 23:58pm    
When I right clicked and selected Copy Image Url I'm getting this http://localhost:1311/Images/9.gif
and in my system the file is present in the folder path D:\Website1\Images\9.gif
idenizeni 31-Jul-13 16:12pm    
If D:\Website1\ is your site's root then the image's path looks correct to me. Are you certain the 9.gif file exists in the images directory?
Mathi2code 1-Aug-13 0:06am    
It is there in the physical path but when I try to right click the default image icon and open in new tab am getting an error saying file not found 404 issue. I'm not even hosting it to IIS I've just create the website appln. and clicked on debug button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900