在datagridview中绘制两个单元格之间的一行 - csharp [英] Drawing a line between two cells in datagridview - csharp

查看:81
本文介绍了在datagridview中绘制两个单元格之间的一行 - csharp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。



我搜索了整个网站,但我找不到任何有用的提示。我的要求是在DataGridView中的两个单元格之间画一条线。



我试图使用CellPaint Event但似乎CellPaint Event只能在一个单元格内绘制,如绘制单元格边框。



如果我想在单元格A与行0和列0之间绘制对角线,单元格B与行10,第10列在同一数据网格视图中绘制,如何实现这一目标?我想用一条线将Cell A的中间点连接到Cell B的中间点。



请分享你的宝贵提示。



提前致谢。



我的尝试:



Hi.

I searched entire website but I could not find any useful tips about this. My request is to draw a line between two cells in DataGridView.

I tried to using CellPaint Event but it seems CellPaint Event only works to draw inside one cell like drawing cell border.

If I want to draw a diagonal line between cell A with row 0 and column 0 and cell B with row 10, column 10 in the same datagridview, How one can achieve this ? I want to connect the middle point of Cell A to the middle point of Cell B using a line.

Please share your valuable tip.

Thanks in advance.

What I have tried:

    int rIndexPrev = lineCellRow[i - 1];
    int cIndexPrev = lineCellColumn[i - 1];

    int rIndexNext = lineCellRow[i];
    int cIndexNext = lineCellColumn[i];



    if (rIndexPrev >= 0 && rIndexNext >= 0)
    {

        try
        {
            DataGridViewCell cellPrev = dataGridView1[rIndexPrev, cIndexPrev];
            DataGridViewCell cellNext = dataGridView1[rIndexNext, cIndexNext];

            int x1 = (cellPrev.ContentBounds.Left + cellPrev.ContentBounds.Right) / 2;
            int y1 = (cellPrev.ContentBounds.Bottom + cellPrev.ContentBounds.Top) / 2;

            int x2 = (cellNext.ContentBounds.Left + cellNext.ContentBounds.Right) / 2;
            int y2 = (cellNext.ContentBounds.Bottom + cellNext.ContentBounds.Top) / 2;



            e.Graphics.DrawLine(linePen, x1, y1, x2, y2);
        }
        catch (Exception ex)
        {

        }
    }

}

推荐答案

您不能(或至少不应该)在Cell绘制方法中执行它 - 它是DGV宽泛的函数,因此它属于DGV Paint事件。

在那里,它非常简单:

You can't (or at least shouldn't) do it in a Cell painting method - it's a DGV wide function, so it belongs in the DGV Paint event.
In there, it's pretty easy:
private void myDataGridView_Paint(object sender, PaintEventArgs e)
    {
    if (myDataGridView.Rows.Count >= 3 && myDataGridView.Columns.Count >= 3)
        {
        Rectangle TL = myDataGridView.GetCellDisplayRectangle(0, 0, false);
        Rectangle BR = myDataGridView.GetCellDisplayRectangle(2, 2, false);

        int x1 = (TL.Left + TL.Right) / 2;
        int y1 = (TL.Bottom + TL.Top) / 2;

        int x2 = (BR.Left + BR.Right) / 2;
        int y2 = (BR.Bottom + BR.Top) / 2;

        e.Graphics.DrawLine(Pens.Red, x1, y1, x2, y2);
        }
    }


我曾经使用透明面板放置在另一个控件的顶部,也许你可以使用绘制对角线。文章中的文字与您无关,只需下载 CustomGroupBox2 代码: Ye Olde GroupBox的新皮肤 [ ^ ]



您可以更改 TransparentPanel 这样的代码:

I once used a transparent Panel to place on top of another control, maybe you can use that to draw your diagonal line in. The text in the article will not be relevant to you, just download the CustomGroupBox2 code: A New Skin for Ye Olde GroupBox[^]

You could change the TransparentPanel code like this:
using System.Drawing;
using System.Windows.Forms;

public class PanelTransparent : Control
{
    /// <summary>
    /// Transparent Panel.
    /// </summary>
    public PanelTransparent()
    {
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        SetStyle(ControlStyles.UserPaint, true);
        SetStyle(ControlStyles.Opaque, true);
        this.BackColor = Color.Transparent;
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle = cp.ExStyle | 0x20;
            return cp;
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        Pen linePen = new Pen(Color.Black);
        e.Graphics.DrawLine(linePen, 0, 0, Width, Height);
    }
}


这篇关于在datagridview中绘制两个单元格之间的一行 - csharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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