日期:2014-05-17 浏览次数:21126 次
DataGridViewImageColumn image = new DataGridViewImageColumn(); image.ImageLayout = DataGridViewImageCellLayout.Zoom; image.HeaderText = "图片描述"; image.Name = "图片描述"; image.Width = 80; this.dgview_Lesson.Columns.Add(image); for (int i = 0; i < this.dgview_Lesson.Rows.Count; i++) { L_Lesson Info = Lessonlist[i] as L_Lesson; if (System.IO.File.Exists(Path.GetDirectoryName(Application.ExecutablePath) + "\\Lesson\\" + Path.GetFileName(Info._Image.ToString()))) { this.dgview_Lesson["图片描述", i].Value = Image.FromFile(Path.GetDirectoryName( Application.ExecutablePath) + "\\Lesson\\" + Path.GetFileName(Info._Image.ToString())); } }
DataGridViewImageColumn image = new DataGridViewImageColumn(); image.ImageLayout = DataGridViewImageCellLayout.Zoom; image.HeaderText = "图片描述"; image.Name = "图片描述"; //把image和数据源属性绑定起来 image.DataPropertyName="_Image"; image.Width = 80; this.dgview_Lesson.Columns.Add(image); //因为原始数据源不是二进制流,只是string,所以需要在显示的处理转换,此处定制cellFormart显示转换事件 this.dgview_Lesson.CellFormatting += new DataGridViewCellFormattingEventHandler(dgview_Lesson_CellFormatting); void dgview_Lesson_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (dgview_Lesson.Columns[e.ColumnIndex].Name == "图片描述") { if (e.Value != null) { //处理你自己的过程,初始进入e.value是string,返回是image e.value=Image.FromFile(e.value); } } }
------解决方案--------------------
我做了个测试:我直接在datagridview中绑定一张图片,显示红X;
后台代码中加载:
private void Form1_Load(object sender, EventArgs e) { dataGridView1["d", 0].Value = new Bitmap(@"C:\Users\xiebin\Desktop\common\1.jpg"); }