Silagy
Silagy

Reputation: 3083

grid view display image

i have a ActivityType field in my db. the values are [1,2,3]. each number represent type of activity. i want to present the type in gridview as image.

i have 3 images

i have tried to do so with template field but i need switch case statement to transfer the number to image url...

Upvotes: 2

Views: 4842

Answers (1)

Akram Shahda
Akram Shahda

Reputation: 14771

You can handle the GridView.RowDataBound event. Use something like the following:

void gridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{

   if(e.Row.RowType == DataControlRowType.DataRow)
   {
       int type = int.Parse(e.Row.Cells[typeCellIndex].Text);
       Image img = (Image) e.Row.Cells[imageCellIndex].FindControl(imageID);
       switch type
       {
          case 1:
          {
             img.ImageUrl = "avtivity_choise.jpg";
          }
          ......
       }

   }

 }

You can also bind your image field to a database field. Refer to the following:

http://msdn.microsoft.com/en-us/library/aa479350.aspx

http://csharpdotnetfreak.blogspot.com/2009/07/display-images-gridview-from-database.html

http://www.codeproject.com/KB/aspnet/imagegalleryingridview.aspx

Upvotes: 2

Related Questions