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

时间:2013-03-22 08:27:08

标签: c# .net datagridview

用途:.NET 2.0+; VS 2005;

我有以下的Custom 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);
        }
    }




}

1 个答案:

答案 0 :(得分: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);
        }