DataGridView图像后的文本在同一单元格;

本文关键字:单元格 文本 图像 DataGridView | 更新日期: 2023-09-27 18:05:10

使用:.NET 2.0+;VS 2005;

我有以下自定义DataGridView;

在这里,我有一个TextandImageCell,但插图总是在像

这样的单元格中按Text在Image之后的顺序排列

(图片|文字)

但是我的要求是在像

这样的单元格中以相反的方式使用它

(Text | Image)

有什么解决方法吗?

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace MyWindowsApplication
{
    public class DataGridViewRadioButtonColumn : DataGridViewCheckBoxColumn
    {
        internal const int radioButtonSize = 14;
        public DataGridViewRadioButtonColumn()
            : base()
        {
            this.ReadOnly = true;
        }
        internal static void Paint(Graphics g, Rectangle cellBounds, bool state)
        {
            cellBounds.Inflate(-1, -1);
            Brush drawBrsh = new SolidBrush(SystemColors.Control);
            g.FillRectangle(drawBrsh, cellBounds);
            cellBounds.Inflate(0, -((cellBounds.Height - radioButtonSize) / 2));
            ControlPaint.DrawRadioButton(g, cellBounds, (state) ? ButtonState.Checked : ButtonState.Normal);
            drawBrsh.Dispose();
        }
    }
    public class DataGridViewRadioButtonCell : DataGridViewCheckBoxCell
    {
    }
    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; }
        }
    }
    public class LinkAndImageColumn : DataGridViewLinkColumn
    {
        private Image imageValue;
        private Size imageSize;
        public LinkAndImageColumn()
        {
            this.CellTemplate = new LinkAndImageCell();
        }
        public override object Clone()
        {
            LinkAndImageColumn c = base.Clone() as LinkAndImageColumn;
            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 LinkAndImageCell LinkAndImageCellTemplate
        {
            get { return this.CellTemplate as LinkAndImageCell; }
        }
        internal Size ImageSize
        {
            get { return imageSize; }
        }
    }
    public class LinkAndImageCell : DataGridViewLinkCell
    {
        private Image imageValue;
        private Size imageSize;
        public override object Clone()
        {
            LinkAndImageCell c = base.Clone() as LinkAndImageCell;
            c.imageValue = this.imageValue;
            c.imageSize = this.imageSize;
            return c;
        }

        public Image Image
        {
            get
            {
                if (this.OwningColumn == null ||  this.OwningLinkAndImageColumn == null)
                {
                    return imageValue;
                }
                else if (this.imageValue != null)
                {
                    return this.imageValue;
                }
                else
                {
                    return this.OwningLinkAndImageColumn.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 LinkAndImageColumn OwningLinkAndImageColumn
        {
            get { return this.OwningColumn as LinkAndImageColumn; }
        }
    }
    public class CustomDataGridView : DataGridView
    {
        private DataGridViewRow _currentSelectedRow = null;
        public DataGridViewRow CurrentSelectedRow
        {
            get { return _currentSelectedRow; }
            set { _currentSelectedRow = value; }
        }
        protected override void OnCellClick(DataGridViewCellEventArgs e)
        {
            bool b = false;
            if (e.RowIndex > -1)
            {
                DataGridViewCell cell = this[e.ColumnIndex, e.RowIndex];
                if (cell.OwningColumn is DataGridViewCheckBoxColumn)
                {
                    if (cell.FormattedValue != null)
                    {
                        for (int i = 0; i < this.RowCount; i++)
                        {
                            this[e.ColumnIndex, i].Value = false;
                        }
                        cell.Value = true;
                    }
                }
            }
            base.OnCellClick(e);
        }
        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            bool b = false;
            if ((e.RowIndex > -1) && (e.ColumnIndex > -1) && (this.Columns[e.ColumnIndex] is DataGridViewRadioButtonColumn))
            {
                e.PaintBackground(e.CellBounds, true);
                if (e.Value != null)
                {
                    if (e.Value.ToString() == "1")
                    {
                        b = true;
                    }
                    else b = false;
                }
                DataGridViewRadioButtonColumn.Paint(e.Graphics, e.CellBounds, b);
                e.Handled = true;
            }
            base.OnCellPainting(e);
        }
        protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                this.CurrentSelectedRow = this.Rows[e.RowIndex];
                this.CurrentCell = this[0, e.RowIndex];
                this.Rows[e.RowIndex].DefaultCellStyle.SelectionBackColor = Color.Gold;
            }
            base.OnCellMouseDown(e);
        }
    }


}

DataGridView图像后的文本在同一单元格;

要在文本后显示图像,您应该将此代码添加/更新到LinkAndImageCell类的Paint方法

if (this.Image != null)
        {
            Size textSize = TextRenderer.MeasureText(value.ToString(), cellStyle.Font, cellBounds.Size);
            int imageXLocation = cellBounds.Location.X + textSize.Width;
            int imageYLocation = cellBounds.Location.Y + textSize.Height / 2 ;
            Point imageLocation = new Point(imageXLocation, imageYLocation);
            GraphicsContainer container = graphics.BeginContainer();
            graphics.SetClip(cellBounds);
            graphics.DrawImageUnscaled(Image, imageLocation);
            graphics.EndContainer(container);
        }