我有一个要求,我需要动态地将图像和文本添加到同一列。我遵循了许多示例,但没有一个有效。也引用了以下内容,但在引用上述文章时出现了转换错误。无法将 System.Windows.Forms.DataGridViewImageCell
类型的对象转换为类型 DataGridViewCustom.TextAndImageCell
。
有人可以帮忙吗?
最佳答案
我在你分享的链接上没有找到任何代码,我会给你我以前用过的代码。
1- 先新建类名吧 TextAndImageColumn.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 个不同的图像(16 x 16).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();
}
引用 How to insert image with Text in one cell of datagridview in C#
关于C# DataGridView 在同一个单元格中显示图像和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26379696/