开发者

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

  • activity type 1 = avtivity_choise.jpg

  • activity type 2 = activity_text.jpg

  • activity type 3 = activity_count.jpg

  • 开发者_开发问答

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜