我一直试图弄清楚这一整天。我试图用3列中的SQL数据加载GridView,然后动态地将图像添加到第4列。来自SQL的数据加载正常,但无论我做什么,都无法加载图像。这里是我的代码:将图像加载到Gridview中
SqlCommand cmd1 = new SqlCommand("StoredProc", myConnection1);
cmd1.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = cmd1.ExecuteReader();
while (reader.Read())
{
if (reader["ServiceStatus"].ToString().ToLower() == "stopped")
{
ImageField status = new ImageField();
status.HeaderText = "Status";
status.Visible = true;
GridView1.Columns.Add(status);
status.DataAlternateTextFormatString = @"~/Images/Image.gif";
}
GridView1.DataSource = reader;
GridView1.DataBind();
}
我已经尝试创建一个数据表并填充它,它仍然不会工作。我知道必须有一个简单的方法来做到这一点。我错过了什么?
编辑:
我使用一个TemplateField现在,我得到一个默认的图像显示出来:
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl="~/Images/greydot.jpg"/>
</ItemTemplate>
</asp:TemplateField>
所以,我应该怎么落后,为了改变引用此IMAGEURL在我的代码图片?图像存储在解决方案中,而不是SQL。
我编辑了您的标题。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 –
您可能还需要设置[DataImageUrlField](http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.imagefield.dataimageurlfield%28v=vs.110%29.aspx)为了显示任何图像,然后在** DataAlternateTextFormatString **([MSDN用法示例](http://msdn.microsoft.com/zh-cn/library/system.web.ui))中设置格式字符串。 webcontrols.imagefield.dataalternatetextformatstring%28V = vs.110%29.aspx))。 – pasty