简体   繁体   English

在GridView中显示数据库中的图像

[英]Display image from database in gridview

I have a GridView that shows information from a building. 我有一个GridView,它显示建筑物的信息。 I am loading the information into the GridView from a database like this: 我正在从这样的数据库中将信息加载到GridView中:

var buildings = (from t in dc.Towns
                         where (t.userid == userid) && (t.time < DateTime.Now) 
                         join b in dc.Buildings
                             on t.buildingid equals b.buildingid
                             into j1
                         from j2 in j1.DefaultIfEmpty()
                         group j2 by j2.buildingname
                         into grouped
                         select new
                                    {
                                        Building= grouped.Key,
                                        Picture= grouped.First().imagePath,
                                        Number= grouped.Count()
                                    });
        gvBuildings.DataSource = buildings.ToList();
        gvBuildings.DataBind();

But I can't display the image, it only shows the path where its saved. 但是我无法显示图像,它仅显示其保存位置。 How can I display the image from the path in the GridView? 如何从GridView中的路径显示图像?

You have to have column type that can display image, use Image Field and set DataImageUrlField property with that URL, look here for details: 您必须具有可以显示图像的列类型,使用图像字段并使用该URL设置DataImageUrlField属性,请在此处查看详细信息:

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

Try loading the images as Image objects with Image.FromFile(string path) and put them into your binding data in place of the path. 尝试使用Image.FromFile(string path)将图像作为Image对象加载,并将其放置在绑定数据中,以代替路径。

eg 例如

                 select new
                            {
                                Building= grouped.Key,
                                Picture= Image.FromFile(grouped.First().imagePath),
                                Number= grouped.Count()
                            });

You'll need some error handling, of course, and I'd also recommend separating your data access code from your display logic, rather than having it all in one method. 当然,您将需要一些错误处理,并且我还建议您将数据访问代码与显示逻辑分开,而不是一味地将其全部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM