1

用途:.NET 2.0+;与 2005 年相比;

我有我的以下自定义 DataGridView;下面提到的代码。

在这个,我有一个TextandImageCell,但插图总是在一个单元格中的图像之后的文本顺序

(图片|文字)

但是我的要求是以相反的方式将它放在像这样的单元格中

(文字 | 图片)

有什么解决方法吗?

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




}
4

1 回答 1

0

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

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);
        }
于 2014-05-26T16:20:44.547 回答