GridView怎样根据数据库中不同的值显示不同的图片
例如:数据库中“img”字段数据值为1的记录显示A图像,为2的记录显示B图像。
我现在用的是一个比较投机的方法,就是把图像名的与记录值名一样<asp:Image ID="img1" runat="server" Height="50px" Width="100px" ImageUrl='<%# Bind("img","images/{0}.jpg") %>' /></td>我想知道正常的方法,谢谢

解决方案 »

  1.   

        protected void gvRoom_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
               
                (e.Row.FindControl("Label2") as Label).Attributes.Add("onmouseover", "ShowDiv(this,'" + Convert.ToString(DataBinder.Eval(e.Row.DataItem, "RoomTypeId")) + "' ,'" + HiddenField1.Value + "','" + HiddenField2.Value + "','" + HiddenField3.Value + "')");
                (e.Row.FindControl("Label2") as Label).Attributes.Add("onmouseout", "CloseDiv()");
                int RoomState = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "RoomState"));            
                switch (RoomState)
                { 
                    case 1:
                        ((Label)e.Row.FindControl("Label1")).Text = "充足";
                        (e.Row.FindControl("image") as Image).ImageUrl = "~/images/fan-3.gif";
                        break;
                    case 2:
                        ((Label)e.Row.FindControl("Label1")).Text = "一般";
                        (e.Row.FindControl("image") as Image).ImageUrl = "~/images/fan-1.gif";
                        break;
                    case 3:
                        ((Label)e.Row.FindControl("Label1")).Text = "紧张";
                        (e.Row.FindControl("image") as Image).ImageUrl = "~/images/fan-4.gif";
                        break;
                    case 4:
                        ((Label)e.Row.FindControl("Label1")).Text = "客满";
                        (e.Row.FindControl("image") as Image).ImageUrl = "~/images/fan-2.gif";
                        break;               
                }
      

  2.   

    不知道你是存的路径还是怎么样。。你可以把主键对应的路径写出来试试?。。字段1   路径 /image/xxx.jsp
    字段2   路径 /image/vvv.jsp这样不知道能不能满足你的要求。。
      

  3.   

    Select Case Img When '1' then 'fan-1.gif'
                    When '2' then 'fan-2.gif' end 
    From tb
      

  4.   

    如固定的话,直接用id主键,
    Select Img Form tb where Id = "+id+";