在c#中设置DataGridViewButtonColumn的图像

时间:2010-10-20 11:02:11

标签: c# winforms datagridview

我想在c#.net应用程序中为DataGridViewButtonColumn设置图像。我通过单元格绘制事件尝试了这个。还有其他方法来设置DataGridViewButtonColumn的图像。  请帮忙

2 个答案:

答案 0 :(得分:5)

你试过这个吗? DataGridView Image Button Cell

答案 1 :(得分:0)

public partial class DataGridViewImageButtonColumn : DataGridViewButtonColumn
{
    public DataGridViewImageButtonColumn() : base()
    {
        base.CellTemplate = new DataGridViewImageButtonCell();
    }

    public override object Clone()
    {
        var col = (DataGridViewImageButtonColumn)base.Clone();
        col.Image = Image;
        return col;
    }

    [DefaultValue(null)]
    [Category("Appearance")]
    [Description("DataGridViewImageButtonColumn_ImageDescr")]
    public Image Image { get; set; }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            if (value != null && !(value.GetType().IsAssignableFrom(typeof(DataGridViewImageButtonCell))))
            {
                throw new InvalidCastException("Must be a DataGridViewImageButtonCell");
            }
            base.CellTemplate = value;
        }
    }
}

public class DataGridViewImageButtonCell : DataGridViewButtonCell
{
    protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
        DataGridViewElementStates elementState, object value,
        object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
        DataGridViewPaintParts paintParts)
    {
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value,
         formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

        var col = (DataGridViewImageButtonColumn)OwningColumn;
        if (col.Image != null)
            graphics.DrawImage(col.Image, CenterInRectangle(col.Image.Size, cellBounds));
    }

    public static Point CenterInRectangle(Size Inner, Rectangle Outer)
    {
        return new Point()
        {
            X = Outer.X + ((Outer.Width - Inner.Width) / 2),
            Y = Outer.Y + ((Outer.Height - Inner.Height) / 2)
        };
    }
}