在单独的表面(即面板)上绘制DataGridView,以创建图层效果 [英] Draw On DataGridView on a separate surface i.e. Panel to create a layer effect

查看:91
本文介绍了在单独的表面(即面板)上绘制DataGridView,以创建图层效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows Forms应用程序中有一个datagridview.在此datagridview上,我想显示一条连接线(以表示某些行之间的分组),从2行到4行.这是出于示例目的,在实际需求中,基于datagridview中的数据,将采用开始和结束行.

现在,问题是我想在单独的表面(如图层)上绘制这条线.因此,datagridview将在一层中,而线条将绘制在另一层上,以便可以关闭此层.

我正在考虑使用如本文

I have a datagridview in a Windows Forms application. On this datagridview I want to show a connector line (to represent grouping between some rows) from say 2 row to 4 row. This is for example purpose, in actual requirement based on the data in the datagridview the starting and ending row will be taken.

Now, the question is I want to draw this line on a separate surface like layer. So, the datagridview will be in one layer, and the lines are to be drawn on another layer, so that this layer can be turned off.

I am thinking of doing the above thing using the Panel control as shown in this article How to Use Transparent Images and Labels in Windows Forms[^] setting the panel to match the size of the datagridview and drawing in the paint event of datagridview so that whenever the datagridview is redrawn the panel is also redrawn.

Can you please tell whether my approach is correct, or is there any other better method.

Thank you in advance.

推荐答案

您根本不需要面板.

通过继承来创建自己的DGV类,然后使用类似以下内容的OnPaint方法进行覆盖:
You don''t need the Panel at all.

Create your own DGV class by inheriting from it, then override the OnPaint method with something like:
Public Class MyDGV
    Inherits DataGridView

    Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)

        '' Call your line painting code here...
    End Sub


    Private Sub MyLinePaintingCode()
        Dim g As Graphics = Me.CreateGraphics

        '' Your painting logic goes here...
    End Sub
End Class

Then you don''t have to use a Panel control and worry about the messy details of Transparency in Windows Forms.

Your custom DGV will show up in the Toolbox.  Use that one instead of the default DGV control and your done.


这篇关于在单独的表面(即面板)上绘制DataGridView,以创建图层效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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