Datagridview单元格与背景图像 [英] Datagridview cell with background image

查看:784
本文介绍了Datagridview单元格与背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个DataGridView,其中一个单元格(DataGridViewTextBoxCell)我想有一个背景图像。为此,我在 CellPainting 事件中使用了以下内容。

  private void dataGridView1_CellPainting(object sender,DataGridViewCellPaintingEventArgs e)
{
var image = Resources.item_qty_white;
e.PaintBackground(e.ClipBounds,false);

e.Graphics.DrawImageUnscaled(image,1410,e.CellBounds.Top);
}

这个效果很好,图像是我想要的位置。但是,它所在的单元格具有带数值的DataGridViewTextBoxCell。图像漂浮在该值的顶部,因此被隐藏。我想,理想的解决方案是使DataGridViewTextBoxCell成为TopMost,但我无法弄清楚如何做到这一点。



然后,我决定尝试使背景图像部分透明,所以下面的值是可视化的,所以我将 CellPainting 代码更改为下面。

  private void dataGridView1_CellPainting(object sender,DataGridViewCellPaintingEventArgs e)
{
var image = Resources.item_qty_white;

e.PaintBackground(e.ClipBounds,false);
image.MakeTransparent(Color.White);
e.Graphics.DrawImageUnscaled(image,1410,e.CellBounds.Top);
}

这再次工作,我可以看到周围的背景图像的值我想要。但是,当我尝试更新单元格的值时,会出现下一个问题。一旦我这样做,以前的值是可见的,我试图设置的新值是重叠的。我现在被困了。



任何建议/指导都将非常感激。

解决方案

您必须设置 e.Handled = true 以防止系统从绘画。以下代码按照您的预期工作。

  void dataGridView1_CellPainting(object sender,DataGridViewCellPaintingEventArgs e)
{
if(e.RowIndex!= - 1&& e.ColumnIndex == columnIndex)
{
if((e.PaintParts& DataGridViewPaintParts.Background)!= DataGridViewPaintParts.None)
{
e。 Graphics.DrawImage(Resources.Image1,e.CellBounds);
}
如果(!e.Handled)
{
e.Handled = true;
e.PaintContent(e.CellBounds);
}
}
}


I have created a DataGridView, one of the cells (DataGridViewTextBoxCell) I would like to have a background image. To do this I have used the following on the CellPainting event.

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        var image = Resources.item_qty_white;
        e.PaintBackground(e.ClipBounds, false);

        e.Graphics.DrawImageUnscaled(image, 1410, e.CellBounds.Top);
    }

This works well and the image is on ever row in the position I want. However, the cell it is sitting in has a DataGridViewTextBoxCell with a numeric value. The image floats on top of this value and thus is hidden. I guess the ideal solution would be to make the DataGridViewTextBoxCell be "TopMost" but I couldn't figure out how to do this.

I then decided to try and make the background image partial transparent so the value underneath would be visual, so I changed my CellPainting code to below.

 private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    {
        var image = Resources.item_qty_white;

        e.PaintBackground(e.ClipBounds, false);
        image.MakeTransparent(Color.White);
        e.Graphics.DrawImageUnscaled(image, 1410, e.CellBounds.Top);
    }

This again worked and I could see the value with the background image surrounding it as I would like. However the next issue arise when I tried to update the value of the cell. Once I did that the previous value was visible and the new value that I was trying to set was overlapping it. I am now stuck.

Any advice/guidance would be very appreciated.

解决方案

You have to set e.Handled = true to prevent the system from painting. The below code works as you expected.

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex != -1 && e.ColumnIndex == columnIndex)
    {
        if ((e.PaintParts & DataGridViewPaintParts.Background) != DataGridViewPaintParts.None)
        {
            e.Graphics.DrawImage(Resources.Image1, e.CellBounds);                    
        }
        if (!e.Handled)
        {
            e.Handled = true;
            e.PaintContent(e.CellBounds);
        }            
    }
}

这篇关于Datagridview单元格与背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆