Is it possible to add images in datagridview cell dynamically?
i am developing a small application in which i have to include images with other data from SQL but the images should be come from local disk depending on file name from database. is i开发者_如何转开发t possible to do it? how?
use TamplateField
<asp:GridView ID="myGridView" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img src="<%#Eval("FileName")%>" alt="NoImage">
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Note you should set image tag source to relative path not physical path
Edit
in case of winforms as you mentioned in your comment you can use DataGridViewImageColumn
in your DataGridVeiw
and set its DataPropertyName
property to a an Image instance.
check my simple example
class MyDataSource
{
// its value come from your database but now I will set it manually
public String imgurl
{
get;
set;
}
public Image img
{
get
{
return Image.FromFile(imgurl);
}
}
}
in from load
List<MyDataSource> mydatasource = new List<MyDataSource>();
mydatasource.Add(new urls() { imgurl = @"C:\image1.jpg" });
mydatasource.Add(new urls() { imgurl = @"C:\image2.jpg" });
mydatasource.Add(new urls() { imgurl = @"C:\image3.jpg" });
mydatasource.Add(new urls() { imgurl = @"C:\image4.jpg" });
//bind this collection to your datagridview
dataGridView1.DataSource = mydatasource;
in this sample I set DataPropertyName
of DataGridViewImageColumn
to img
property
If I understood your question correctly, you have the image urls in the database and want to display those images in the gridview. If that is so, my suggestion would be to add an "asp:Image" tag to the gridview statically and update the "ImageUrl" attr using "Bind" method.
Hope this helps!!
精彩评论