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

时间:2014-10-15 10:00:54

标签: c# datagridview

我有一个要求,我需要动态地将图像和文本添加到同一列。我已经遵循了很多例子,但没有一个是有效的。提到以下也是如此,但在参考上述文章时得到了投射错误。无法将System.Windows.Forms.DataGridViewImageCell类型的对象转换为DataGridViewCustom.TextAndImageCell类型。

http://akhaliq.com/?p=82

有人可以为此提供帮助吗?

4 个答案:

答案 0 :(得分:4)

我在你分享的链接上找不到任何代码,我会给你以前用过的代码。

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-然后在按钮Click事件上使用Text和Image单元格在网格行上添加数据。

    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#

答案 1 :(得分:0)

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

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

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

        }
    }

答案 2 :(得分:-1)

这是更好的

                graphics.DrawImageUnscaled(this.Image, new Point(cellBounds.Location.X + 2, cellBounds.Location.Y + ((cellBounds.Height-this.Image.Height)/2)));

答案 3 :(得分:-1)

您可以将DataGridView单元格动态转换为DataGridViewTextBoxCell()并将文本值显示到该列。下面是代码示例,它为您提供了一些基本想法。

private void DisplayButton_Click(object sender, EventArgs e)
{
    dataGridView1.Rows[3].Cells["ImageCol"] = new DataGridViewTextBoxCell();
    dataGridView1.Rows[3].Cells["ImageCol"].Value = "Hello..";  
}
相关问题