Cellpainting复选框或绘制单元格的一部分 [英] Cellpainting checkbox or paint part of the cell

查看:142
本文介绍了Cellpainting复选框或绘制单元格的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Winforms中使用DataGridView遇到这种情况。
我想达到这样的效果,行之间有一些分隔

I have this situation with DataGridView in Winforms. I want to achive something like this, with some separation between rows

图像和地址N39实际上位于2列上,没有水平边距,也没有列网格线看起来它们属于一个整体。为了实现行之间的分隔,我重写了CellPainting并使用从Graphics.FillRectangle(高度小于单元格高度),Graphics.DrawLine到Graphics.DrawString和Graphics.DrawText的单元格内容重绘整个网格。

The image and address N39 are actually on 2 columns, with no horizontal margin and no column grid line to make it look like they belong to a whole. To achieve sepration between rows, I override CellPainting and repaint the whole grid, using from Graphics.FillRectangle (with height less than cell height), Graphics.DrawLine to Graphics.DrawString and Graphics.DrawText to draw cell contents.

在第二种情况下,我将复选框作为列之一。

In the second scenario, I have checkbox as one of the columns.

如果我不重新粉刷,单元格将接触白色网格线,并且行之间不留任何间隔。

If I don't repaint, the cell will touch the white grid lines and leave no separation between rows.

但是,如果我想要覆盖CellPainting以创建类似上述的分隔,我可能需要重新绘制整个内容,但是我不知道有任何绘制和处理复选框的方法。
所以我在想如果可能的话,我会在单元格上绘制一个带有背景颜色(浅灰色)的封闭矩形,并将Winform的内容留给句柄。

However if I want to override CellPainting to create some separation like above, I may need to repaint the whole content but I am not aware of any method to draw and handle checkbox. So I was thinking if possible, I would just draw an enclosing rectangle with background color (light gray) to the cells and leave the content for Winform for handle.

我的问题是:


  1. 如何只重涂一部分油漆细胞的,例如绘制一个封闭的矩形,然后让其余单元格内容供Winforms处理?

  1. How can I repaint only part of the cell, e.g. draw an enclosing rectangle, and let the remaining cell content for Winforms to handle?

如果我们别无选择,只能重新绘制整个单元格,那么如何重新绘制并处理复选框列?

If we have no choice but to repaint the whole cell, how do you repaint and handle checkbox column?

欢迎其他任何建议。

非常感谢。

最好。

推荐答案

CellPainting 事件的无处不在的 e 中,丰富的参数集中有两种便捷的方法:

There are two handy methods in the rich set of parameters in the ubiquous e of the CellPainting event:

e.PaintContent e.PaintBackground 完成所有操作如果您只想更改外观的一部分,请执行以下操作:

e.PaintContent and e.PaintBackground do all the work if you want to change only a part of the looks:

if (e.ColumnIndex == yourColumnIndex)
{
    // do your special stuff..
    e.Graphics.FillRectangle(Brushes.Wheat, e.CellBounds);
    // ..
    // now get the regular content drawn by the system
    e.PaintContent(e.CellBounds);  
    // and quit
    e.Handled = true;
}

这篇关于Cellpainting复选框或绘制单元格的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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