
要实现该功能,可通过非绑定列的方式来实现。具体实现方法如下: 1 创建了一个非绑定列并设置其相应的属性,属性设置如下: FieldName设为 Image (该字段名必须是唯一的) UnboundType设为 UnboundColumnTypeObject ColumnEdit设为RepositoryItemPictureEdit类的实例(该 *** 作PictureEdit 为该列的内置编辑器) 2 处理View的CustomUnboundColumnData事件,用于为非绑定列填充数据。 关键代码: //获取文件路径 string GetFileName(string color) { if(color == null || color == stringEmpty) return stringEmpty; return color + "jpg"; } //处理CustomUnboundColumnData事件,为非绑定列填充数据 private void gridView1_CustomUnboundColumnData(object sender, DevExpressXtraGridViewsBaseCustomColumnDataEventArgs e) { if(eColumnFieldName == "Image" && eIsGetData) { GridView view = sender as GridView; string colorName = (string)((DataRowView)eRow)["Color"]; string fileName = GetFileName(colorName)ToLower(); if(!ImagesContainsKey(fileName)) { Image img = null; try { string filePath = DevExpressUtilsFilesHelperFindingFileName(ApplicationStartupPath, ImageDir + fileName, false); img = ImageFromFile(filePath); } catch { } ImagesAdd(fileName, img); } eValue = Images[fileName]; } } 点击下载示例 本站文章除注明转载外,均为本站原创或翻译
仅供参考:
private void FormChart_Load(object sender, EventArgs e){
//生成DataTable
DataTable dta = new DataTable("Test");
dtaColumnsAdd("ID", typeof(SystemInt32));
dtaColumnsAdd("Name", typeof(SystemString));
dtaColumnsAdd("Address", typeof(SystemString));
dtaColumnsAdd("Image",typeof(SystemByte[]));
//路径
string filePath = AppDomainCurrentDomainBaseDirectory + "\\image\\Status_OKpng";
Image img = new Bitmap(filePath);
byte[] imgBytes = ImageToBytes(img);
dtaRowsAdd(new object[] { 1, "张三", "北京市海淀区" , imgBytes });
dtaRowsAdd(new object[] { 1, "李四", "北京市朝阳区", imgBytes });
dtaRowsAdd(new object[] { 1, "王五", "北京市东城区", imgBytes });
dtaRowsAdd(new object[] { 1, "赵六", "北京市西城区", imgBytes });
dtaAcceptChanges();
//设置行高
gvRowHeight = 32;
//格式化列
RepositoryItemPictureEdit pic = new RepositoryItemPictureEdit();
//居中
picPictureAlignment = ContentAlignmentMiddleCenter;
//垂直拉伸
picSizeMode = DevExpressXtraEditorsControlsPictureSizeModeZoom;
gvColumns["Image"]ColumnEdit = pic;
//绑定
gridDataSource = dta;
}
/// <summary>
/// Image 转成 Byte[]
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static byte[] ImageToBytes(Image image)
{
ImageFormat format = imageRawFormat;
using (MemoryStream ms = new MemoryStream())
{
if (formatEquals(ImageFormatJpeg))
{
imageSave(ms, ImageFormatJpeg);
}
else if (formatEquals(ImageFormatPng))
{
imageSave(ms, ImageFormatPng);
}
else if (formatEquals(ImageFormatBmp))
{
imageSave(ms, ImageFormatBmp);
}
else if (formatEquals(ImageFormatGif))
{
imageSave(ms, ImageFormatGif);
}
else if (formatEquals(ImageFormatIcon))
{
imageSave(ms, ImageFormatIcon);
}
byte[] buffer = new byte[msLength];
//ImageSave()会改变MemoryStream的Position,需要重新Seek到Begin
msSeek(0, SeekOriginBegin);
msRead(buffer, 0, bufferLength);
return buffer;
}
}
以上就是关于如何在DevExpress GridControl的一列中显示外部图片全部的内容,包括:如何在DevExpress GridControl的一列中显示外部图片、MFC cgridctrl 合并单元格、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)