DataGridView一个格子中显示图片和文字,请高手帮忙。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace JrtGenerator.Utility
{
public class TextAndImageColumn : DataGridViewTextBoxColumn
{
private Image imageValue;
private Size imageSize;
public TextAndImageColumn()
{
this.CellTemplate = new TextAndImageCell();
}
public override object Clone()
{
TextAndImageColumn c = base.Clone() as TextAndImageColumn;
c.imageValue = this.imageValue;
c.imageSize = this.imageSize;
return c;
}
public Image Image
{
get
{
return this.imageValue;
}
set
{
if (this.Image != value)
{
this.imageValue = value;
this.imageSize = value.Size;
if (this.InheritedStyle != null)
{
Padding inheritedPadding = this.InheritedStyle.Padding;
this.DefaultCellStyle.Padding = new Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom);
}
}
}
}
private TextAndImageCell TextAndImageCellTemplate
{
get
{
return this.CellTemplate as TextAndImageCell;
}
}
internal Size ImageSize
{
get
{
return imageSize;
}
}
}
public class TextAndImageCell : DataGridViewTextBoxCell
{
private Image imageValue;
private Size imageSize;
public override object Clone()
{
TextAndImageCell c = base.Clone() as TextAndImageCell;
c.imageValue = this.imageValue;
c.imageSize = this.imageSize;
return c;
}
public Image Image
{
get
{
if (this.OwningColumn == null || this.OwningTextAndImageColumn == null)
{
return imageValue;
}
else if (this.imageValue != null)
{
return this.imageValue;
}
else
{
return this.OwningTextAndImageColumn.Image;
}
}
set
{
if (this.imageValue != value)
{
this.imageValue = value;
this.imageSize = value.Size;
Padding inheritedPadding = this.InheritedStyle.Padding;
this.Style.Padding = new Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom);
}
}
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
// Paint the base content
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
if (this.Image != null)
{
// Draw the image clipped to the cell.
System.Drawing.Drawing2D.GraphicsContainer container = graphics.BeginContainer();
graphics.SetClip(cellBounds);
graphics.DrawImageUnscaled(this.Image, cellBounds.Location);
graphics.EndContainer(container);
}
}
private TextAndImageColumn OwningTextAndImageColumn
{
get
{
return this.OwningColumn as TextAndImageColumn;
}
}
}
}
可是,它只实现了,在这列中添加一个Image属性,而我想在这一列的每一个格子中显示不同的图片,就是说最好可以像
this.datagridview.rows.add(??) ??处就可以包含图片的路径 和 文字的string ?
相关推荐
DataGridView中同一个Cell中显示图片和文字
在DataGridView中,通过CellPainting事件来重新绘制单元格,来实现同一个单元格中显示图片和文字。
WinForms自定义DataGridView列:状态选择列的完整实现
DataGridView是WinForms中用途广泛的数据表格控件,默认提供文本、下拉框、按钮等基础列类型,但在处理业务语义丰富的交互时常常力不从心。要突破这种限制,关键在于理解DataGridViewColumn、DataGridViewCell与编辑控件三者之间的协作协议:列负责模板定义,单元格负责绘制和值绑定,编辑控件通过IDataGridViewEditingControl接口接入DataGridView的编辑生命周期。这种自定义列机制不仅能在单元格中自由绘制图形与文本,还能弹出完全自定义的编辑面板
WinForms中DataGridView单元格合并实战:基于CellPainting重绘方案
数据表格控件是桌面应用开发中的高频组件,尤其是WinForms的DataGridView,几乎每个项目都会用到。当业务报表要求将相同值连续单元格合并显示时,DataGridView原生却不支持,让不少人卡在如何实现真正可落地的合并效果。其实,合并的底层原理并不复杂:通过CellPainting事件对单元格进行自定义重绘,遮挡内部边框并统一绘制文本,就能在视觉上实现“合并单元格”。这种方案轻量灵活,无需引入重型第三方控件,特别适合处理纵向合并和横向合并等规则性报表需求。无论是订单统计、部门汇总还是跨字段合并,
Winform在DataGridView中显示图片
首先,要添加图片列,绑定数据的时候会触发CellFormatting事件,在事件中取出图片路径,读取图片赋值给当前单元格。 private void dataGridview1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (dataGridview1.Columns[e.ColumnIndex].Name.Equals(Image)) { string path = e.Value.ToString(); e.Value = GetImage(path);
图片存储到数据库保存二进制文件,并在DATAGRIDVIEW中显示出来
图片存储到数据库保存二进制文件,并在DATAGRIDVIEW中显示出来


