GridControl 实现图片列
Posted Jia_ShengJie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GridControl 实现图片列相关的知识,希望对你有一定的参考价值。
实现代码如下(图片):
private void FormChart_Load(object sender, EventArgs e)
//生成DataTable
DataTable dta = new DataTable("Test");
dta.Columns.Add("ID", typeof(System.Int32));
dta.Columns.Add("Name", typeof(System.String));
dta.Columns.Add("Address", typeof(System.String));
dta.Columns.Add("Image",typeof(System.Byte[]));
//图片路径
string filePath = AppDomain.CurrentDomain.BaseDirectory + "\\\\image\\\\Status_OK.png";
Image img = new Bitmap(filePath);
byte[] imgBytes = ImageToBytes(img);
dta.Rows.Add(new object[] 1, "张三", "北京市海淀区" , imgBytes );
dta.Rows.Add(new object[] 1, "李四", "北京市朝阳区", imgBytes );
dta.Rows.Add(new object[] 1, "王五", "北京市东城区", imgBytes );
dta.Rows.Add(new object[] 1, "赵六", "北京市西城区", imgBytes );
dta.AcceptChanges();
//设置行高
gv.RowHeight = 32;
//格式化图片列
RepositoryItemPictureEdit pic = new RepositoryItemPictureEdit();
//图片居中
pic.PictureAlignment = ContentAlignment.MiddleCenter;
//图片垂直拉伸
pic.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;
gv.Columns["Image"].ColumnEdit = pic;
//绑定
grid.DataSource = dta;
/// <summary>
/// Image 转成 Byte[]
/// </summary>
/// <param name="image">图片</param>
/// <returns></returns>
public static byte[] ImageToBytes(Image image)
ImageFormat format = image.RawFormat;
using (MemoryStream ms = new MemoryStream())
if (format.Equals(ImageFormat.Jpeg))
image.Save(ms, ImageFormat.Jpeg);
else if (format.Equals(ImageFormat.Png))
image.Save(ms, ImageFormat.Png);
else if (format.Equals(ImageFormat.Bmp))
image.Save(ms, ImageFormat.Bmp);
else if (format.Equals(ImageFormat.Gif))
image.Save(ms, ImageFormat.Gif);
else if (format.Equals(ImageFormat.Icon))
image.Save(ms, ImageFormat.Icon);
byte[] buffer = new byte[ms.Length];
//Image.Save()会改变MemoryStream的Position,需要重新Seek到Begin
ms.Seek(0, SeekOrigin.Begin);
ms.Read(buffer, 0, buffer.Length);
return buffer;
实现代码如下(图片+文字):
private void FormChart_Load(object sender, EventArgs e)
//生成DataTable
DataTable dta = new DataTable("Test");
dta.Columns.Add("ID", typeof(System.Int32));
dta.Columns.Add("Name", typeof(System.String));
dta.Columns.Add("Address", typeof(System.String));
dta.Columns.Add("ImageTxt", typeof(System.Int32));
dta.Rows.Add(new object[] 1, "张三", "北京市海淀区", 0 );
dta.Rows.Add(new object[] 1, "李四", "北京市朝阳区", 1 );
dta.Rows.Add(new object[] 1, "王五", "北京市东城区", 2 );
dta.Rows.Add(new object[] 1, "赵六", "北京市西城区", 0 );
dta.AcceptChanges();
ImageList imgList = new ImageList();
imgList.TransparentColor = Color.Transparent;
imgList.ImageSize = new Size(16, 16);
imgList.ColorDepth = ColorDepth.Depth32Bit;
Image img0 = new Bitmap(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "image\\\\Status_OK.png"));
Image img1 = new Bitmap(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "image\\\\Status_Dis.png"));
Image img2 = new Bitmap(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "image\\\\Alarm_OK.png"));
imgList.Images.Add("0", img0);
imgList.Images.Add("1", img1);
imgList.Images.Add("2", img2);
RepositoryItemImageComboBox riImgCmb = new RepositoryItemImageComboBox();
riImgCmb.Items.AddRange(new ImageComboBoxItem[]
new ImageComboBoxItem("链接成功",0,0),
new ImageComboBoxItem("断开连接",1,1),
new ImageComboBoxItem("故障",2,2));
riImgCmb.SmallImages = imgList;
gv.Columns["ImageTxt"].ColumnEdit = riImgCmb;
//绑定
grid.DataSource = dta;
以上是关于GridControl 实现图片列的主要内容,如果未能解决你的问题,请参考以下文章
DevExpress中GridControl列转义的实现方法