DevExpress GridView表格CustomUnboundColumnData事件异步加载图片
DevExpress GridView表格CustomUnboundColumnData事件异步加载图片
图片列设置 1. 图片列的ColumnEdit属性设置为RepositoryItemPictureEdit组件。 repImage.Name = "repImage"; repImage.NullText = " "; 2. 图片列的UnboundType属性设置为Object 窗体定义一个变量images C# Code: Dictionary<string, AsynDownImage> images = new Dictionary<string, AsynDownImage>(); //来源:C/S框架网 | www.csframework.com | QQ:23404761 CustomUnboundColumnData事件: C# Code: private void gvSummary_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) { if ((e.Column.Name == "col_Last1_mainImageUrl" || e.Column.Name == "col_Last2_mainImageUrl") && e.IsGetData) { string imgurl = ""; switch (e.Column.Name) { case "col_Last1_mainImageUrl": imgurl = (e.Row as DataRowView).Row["Last1_mainImageUrl"].ToStringEx(); break; case "col_Last2_mainImageUrl": imgurl = (e.Row as DataRowView).Row["Last2_mainImageUrl"].ToStringEx(); break; } if (String.IsNullOrEmpty(imgurl)) { e.Value = null; return; } if (imgurl.Substring(0, 2) == "//") imgurl = "https:" + imgurl; if (images.ContainsKey(imgurl)) { images[imgurl].AddListener(e); } else { AsynDownImage down = new AsynDownImage(imgurl, this); down.AddListener(e); images[imgurl] = down; } } } //来源:C/S框架网 | www.csframework.com | QQ:23404761 GridView异步加载图片,在UnboundColumn列显示图片 C# Code: /// <summary> /// GridView异步加载url地址的图片,通知CustomColumnDataEventArgs参数 /// </summary> public class AsynDownImage { private string _URL { get; set; } private Bitmap _Image { get; set; } private bool _IsReady = false; private object _Locker = new object(); private List<DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs> _listens; private Control _owner; public AsynDownImage(string url, Control owner) { _owner = owner; _listens = new List<DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs>(); _URL = url; this.StartDown(); } void StartDown() { Task.Run(() => { //判断图片路径是否为网络路径 if (IsUrl(this._URL)) { try { //读取文件 using (WebClient wc = new WebClient()) { Stream tream = wc.OpenRead(this._URL); this._Image = new Bitmap(tream); this._Image = new Bitmap(this._Image, new Size(60, 60)); } } catch (Exception ex) { this._Image = null; } lock (_Locker) { _IsReady = true; } RunNotify(); } }); } public void AddListener(DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs obj) { lock (_Locker) { _listens.Add(obj); if (this._IsReady) { obj.Value = this._Image; } } if (this._IsReady) _owner.Invoke(new DownImageCallback(callback), obj);//立即刷新,显示图片 } void RunNotify() { lock (_Locker) { foreach (var o in _listens) { o.Value = this._Image; } } _owner.Invoke(new DownImageCallback(callback), new CustomColumnDataEventArgs(null, -1, null, false));//立即刷新,显示图片 } private void callback(CustomColumnDataEventArgs e) { if (e != null && e.Column != null) { GridView view = (GridView)e.Column.View; int h = view.GetRowHandle(e.ListSourceRowIndex); if (h >= 0) { view.InvalidateRowCell(h, e.Column);//刷新单个图片单元格 } else { view.Invalidate(); } } } /// <summary> /// 识别urlStr是否是网络路径 /// </summary> /// <param name="url"></param> /// <returns></returns> public static bool IsUrl(string url) { if (Regex.IsMatch(url, @"((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?")) { return true; } else { return false; } } } public delegate void DownImageCallback(CustomColumnDataEventArgs e); //来源:C/S框架网 | www.csframework.com | QQ:23404761 扫一扫加微信
参考文档:
快速增加表格栏位到DevExpres.GridControl.GridView GridView表格中绑定LookupEdit单选组件 解决:DevExpress.GridControl.GridView表格导出乱码 GridView.OptionsView.ShowButtonMode 表格内显示内置按钮图标 表格GridView.CustomDrawEmptyForeground事件显示没有查询到数据 DevExpress GridView设置单元格网格线颜色|C/S框架网 Winform皮肤框架 - GridView表格样式管理器详解|C/S框架网 Winform皮肤框架之GridView表格列头右键菜单详解|C/S框架网 DevExpress表格某个列不显示数据解决方案(图) DevExpress GridView表格记录位置调整,实现DataRow记录排序 DevExpress GridView单元格CellValueChanged事件详解 C# DevExpress会计凭证表格金额录入组件(支持元角分) DevExpress GridView在只读状态下如何隐藏表格的按钮EditorShowMode|ShowButtonMode DevExpress GridView 表格内嵌ButtonEdit按钮列图片按钮及Click事件 C# Dev GridView表格的CustomUnboundColumnData事件异步加载图片类AsynDownImage
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |