C#DataGridView在同一单元格中显示图像和文本
本文关键字:显示图 图像 文本 显示 单元格 C#DataGridView | 更新日期: 2023-09-27 18:26:37
我有一个需求,需要将图像和文本动态添加到同一列中。我列举了许多例子,但没有一个奏效。也参考了以下内容,但在参考上述文章时出现了强制转换错误。无法将System.Windows.Forms.DataGridViewImageCell
类型的对象强制转换为DataGridViewCustom.TextAndImageCell
类型。
http://akhaliq.com/?p=82
有人能帮忙吗?
我在您共享的链接上没有找到任何代码,我会给您以前使用过的代码。
1-首先创建新类名itTextAndImageColumn.cs
:
这是分类代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace TradeGrid
{
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; }
}
}
}
2-之后单击Datagridview
的编辑列(在设计模式下),然后将列类型:DataGridViewTextBoxColumn
更改为柱类型:TextAndImageColumn
3-使用用户控件添加一个图像列表,其中包含2个不同的Images(16x16).png文件。
4-然后添加一种方法,在DataGridView
:的一个单元格中显示图像和文本值
public void ImageRowDisplay()
{
((TextAndImageCell)_TradeGrid.Rows[0].Cells[0]).Image = (Image)imageList1.Images[1];
}
5-然后在网格行上添加数据,并在按钮单击事件上显示"文本和图像"单元格。
private void btnInsertData_Click(object sender, EventArgs e)
{
//Code to insert rows on the grid.
ImageRowDisplay();
}
参考如何在C#中的datagridview的一个单元格中插入带文本的图像
当您在运行时更改行高时,您还必须处理这种情况,因为图像仍然与单元格顶部对齐。
为了避免这种情况,您可以更新位置:
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);
// ====> Recalculate Location to have a Middle alignment
int verticalPosition = cellBounds.Y + ( (cellBounds.Height/2) - (this.Image.Height/2) );
cellBounds.Location = new Point(cellBounds.X, verticalPosition);
graphics.DrawImageUnscaled(this.Image, cellBounds.Location);
graphics.EndContainer(container);
}
}
这是更好的
graphics.DrawImageUnscaled(this.Image, new Point(cellBounds.Location.X + 2, cellBounds.Location.Y + ((cellBounds.Height-this.Image.Height)/2)));
您可以将DataGridView单元格动态转换为DataGridViewTextBoxCell(),并显示该列的文本值。下面的代码示例为您提供了一些基本的想法。
private void DisplayButton_Click(object sender, EventArgs e)
{
dataGridView1.Rows[3].Cells["ImageCol"] = new DataGridViewTextBoxCell();
dataGridView1.Rows[3].Cells["ImageCol"].Value = "Hello..";
}