C#DataGridView在同一单元格中显示图像和文本

C#DataGridView在同一单元格中显示图像和文本,c#,datagridview,C#,Datagridview,我需要在同一列中动态添加图像和文本。我遵循了许多例子,但没有一个是有效的。也参考了以下内容,但在参考上述文章时出现了强制转换错误。无法将类型为System.Windows.Forms.DataGridViewImageCell的对象强制转换为类型DataGridViewCustom.TextAndImageCell 有人能帮忙吗?我在你共享的链接上没有找到任何代码,我会给你我以前用过的代码 1-首先创建新的类名itTextAndImageColumn.cs: 这是类代码: using Syst

我需要在同一列中动态添加图像和文本。我遵循了许多例子,但没有一个是有效的。也参考了以下内容,但在参考上述文章时出现了强制转换错误。无法将类型为
System.Windows.Forms.DataGridViewImageCell
的对象强制转换为类型
DataGridViewCustom.TextAndImageCell


有人能帮忙吗?

我在你共享的链接上没有找到任何代码,我会给你我以前用过的代码

1-首先创建新的类名it
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
更改为列类型
文本和图像列

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();
    }
参考

这样比较好

                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..";  
}

在运行时更改行高度时,还必须处理这种情况,因为图像仍与单元格顶部对齐

要避免这种情况,您可以更新位置:

    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);

        }
    }

您从哪里获得异常?发布代码让生活更轻松。。。你能添加异常的堆栈跟踪吗。