DataGridViewCell更改错误图标

时间:2015-07-14 14:53:36

标签: c# winforms datagridview .net-4.0

我正在使用.Net 4.0,并且当我将错误文本设置为单词"链接"时,我正在寻找一种在单元格中更改错误图像的方法。我还希望显示原始错误图像,如果它是任何其他错误文本。我在设置文本时尝试设置图像,但我无法这样做。

到目前为止,这就是我的全部内容:

protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
    if (e.ErrorText == "linked")
    {
        //Image
    }
    else
    {
        base.OnCellPainting(e);
    }
}

P.S。我之前从未真正使用过覆盖,所以我完全清楚我所做的事情可能完全错了。感谢您的阅读

2 个答案:

答案 0 :(得分:0)

您可以尝试以下代码来更改DataGridViewTextBoxCell ...

的错误图标
class DataGridViewCell: DataGridViewTextBoxCell
{
        protected override void PaintErrorIcon(Graphics graphics, Rectangle clipBounds, Rectangle cellValueBounds, string errorText)
        {
            if(errorText == "linked")
            {
                graphics.DrawIcon(SystemIcons.Error, new Rectangle(cellValueBounds.Width - 10, 0, 10, 10));
            }
            else 
            {
                // base method
            }
        }    
}

For more Infomration click here...

答案 1 :(得分:0)

即使这篇文章有几年的历史,我仍然遇到同样的问题:我的自定义DataGridViewCell实现的PaintErrorIcon覆盖没有被调用。

在谷歌上搜索,并在DataGridViewTextBoxCell上反映,我找到了一个很好的答案,让我得到以下代码。关键是覆盖paint方法并从paintParts参数中删除ErrorIcon绑定标志。

public class DataGridViewCustomErrorIconCell : DataGridViewTextBoxCell
    {
        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)
        {
            // No matter what, do not let the base class see the ErrorIcon Flag. So remove it. The PaintErrorIcon method will never be called by the based class.
            // A benefit of doing this allows us to NOT set DataGridView.ShowErrorIcons = false. (e.g. other cell types will use the default implemention of error icon. 
            // However, JUST FOR THIS COLUMN OF CELLS, we will implement our own call to PainErrorIcon.
            paintParts ^= DataGridViewPaintParts.ErrorIcon; //Removes the flag.

            // Call the base class method to paint the default cell appearance.
            // Since we removed the errorIcon flag, the icon will NEVER EVER EVER be painted for this cell type. 
            // Cool. We will do it ourselves.
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
                value, formattedValue, errorText, cellStyle,
                advancedBorderStyle, paintParts);

            // Since we have removed the ErrorIcon painPart, let's implement the call to PaintErrorIcon ourselves.
            // Note: When using reflector, I saw the base class also checks for the existence of the errorText parameter and if so, calls PaintErrorIcon. (however, with other internal parameters that keep our override from being called. There is a private void PaintPrivate(...) method in the base class that takes 2 extra parameters of type bool called 'computeContentBounds' and 'computeErrorBounds'. And then lots of "if" conditions. 
            if (!string.IsNullOrEmpty(errorText))
            {
                var errorIconBounds = ComputeErrorIconBounds(cellBounds); // As copied from the base class when using reflector.
                PaintErrorIcon(graphics, clipBounds, errorIconBounds, errorText);
            }
        }

        protected override void PaintErrorIcon(Graphics graphics, Rectangle clipBounds, Rectangle errorIconBounds, string errorText)
        {
            // You can use something built-in, such as:
            // graphics.DrawIcon(System.Drawing.SystemIcons.Asterisk, errorIconBounds);

            // *** OR: you can Bob Ross it up and create your own canvas and draw whatever pretty pictures you want, given the errorIconBounds.
            var iconCanvas = new Bitmap(errorIconBounds.Width, errorIconBounds.Height);
            var iconArtist = Graphics.FromImage(iconCanvas); // this artist is assigned to the given canvas.

            // Tell the artist draw something, using a RED brush. Not creative, maybe a Jurassic park video brush? "Uh, uh, uh..."
            iconArtist.DrawString("X", new Font(this.InheritedStyle.Font.FontFamily, 12, FontStyle.Bold, GraphicsUnit.Pixel), Brushes.Red, 0, 0);

            // Then draw that canvas to the error icon bounds.
            graphics.DrawImage(iconCanvas, errorIconBounds);

        }

        private Rectangle ComputeErrorIconBounds(Rectangle cellValueBounds)
        {
            // 12, and 11 are the "defaults" found when reflecting DataGridViewCell and found internal static properties.
            int defaultWidth = 12, defaultHeight = 11;

            if (cellValueBounds.Width < 8 + defaultWidth || cellValueBounds.Height < 8 + defaultHeight)
            {
                return Rectangle.Empty;
            }

            Rectangle rectangle = new Rectangle((cellValueBounds.Right - 4 - defaultWidth), cellValueBounds.Y + (cellValueBounds.Height - defaultHeight) / 2, (int)defaultWidth, (int)defaultHeight);
            return rectangle;
        }
    }

    public class DataGridViewCustomErrorIconColumn : DataGridViewTextBoxColumn
    {
        public DataGridViewCustomErrorIconColumn()
        {
            this.CellTemplate = new DataGridViewCustomErrorIconCell();
        }
    }