views:

1171

answers:

1

I have a gridview and the one coulmn is a image column. How would i change the DataImageUrlFormatString value in my code behind?

i tried doing this but it does not work.......

((ImageField)(GridView2.Rows[0].Cells[0].FindControl("ID"))).DataImageUrlFormatString 

 = "~/createthumb.ashx?gu=/pics/gmustang06_2.jpg";
+2  A: 

Try this:

 ((System.Web.UI.WebControls.Image)(GridView2.Rows[0].Cells[0].Controls[0])).ImageUrl = "~/createthumb.ashx?gu=/pics/gmustang06_2.jpg";

EDIT:

You can set the URL of the path to an image that will be displayed in the image control with declarative syntax:

<asp:ImageField DataImageUrlField="id" DataImageUrlFormatString="img{0}.jpg"></asp:ImageField>

or in the Code Behind by handling the OnRowDataBound event of the GridView control:

protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Image img = e.Row.Cells[0].Controls[0] as Image;
        img.ImageUrl = "img" + DataBinder.Eval(e.Row.DataItem, "id") + ".jpg";
    }      
}
Phaedrus
Thanks it does work, but what must i do and where if i want to change the Image field of each cell and not just on cell [0]. Like i have a Image field in my GridView and a ID field that i get from the DB. Do if the ID is 1 then i want to diplay Image1.jpg and if ID is 2 then i want to display Image2.jpg, and so on and so on. Will this have to be done in my RowDataBound event?
Etienne